编程语言
首页 > 编程语言> > 小渣渣学习笔记 python day69 【Django】

小渣渣学习笔记 python day69 【Django】

作者:互联网

Django

web框架本质

浏览器把你输入的网址,做成请求(请求头),如果是域名,通过dns解析服务器解析出对方服务器地址,然后把请求通过socket发送到对方地址,对方服务器socket接收到请求,查询相应的文件,返回结果(成功 200)并把文件返回到发送端的socket,然后呈现到浏览器比如:
http://www.jd.com/245165220002.html
www.jd.com 是服务器域名,会被dns解析成ip地址
245165220002.html才是真正要请求的路径文件

一个web服务器的简单例子

<!--html文件-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>

</head>
<body>
<h1>hello world</h1>
</body>
</html>
#python server文件,
import socket
sc = socket.socket()
sc.bind(('127.0.0.1',8080))
sc.listen()
while 1:
    conn,addr = sc.accept()
    data = conn.recv(1024) #接收请求头
    print(data.decode('utf-8')) #解码
    conn.send('HTTP/1.1 200 OK \r\n\r\n'.encode('utf-8')) #发送响应结果
    with open('test.html','rb') as f1: #发送文件给浏览器
        conn.send(f1.read())
    conn.close()
先运行起来python文件,浏览器输入 127.0.0.1:8080 ,服务器返回一个html文件,并在浏览器终端显示

请求和响应格式

上述的例子在server的控制台有如下的记录,这个是浏览器的请求头,其中第一行GET / HTTP/1.1中,GET是请求方法,/是个请求的文件, HTTP/1.1是协议

标准的请求格式:

升级的web框架

#上面的html太简单,如果增加一些引用,比如样式:<link rel="stylesheet" href="css1.css"> ,样式文件中增加一些背景色,让界面复杂一些,就要更改下server端,把css文件也发送到客户端:
import socket
sc = socket.socket()
sc.bind(('127.0.0.1',8080))
sc.listen()
while 1:
    conn,addr = sc.accept()
    data = conn.recv(1024)
    request_file = data.decode('utf-8').split('\r\n')[0].split()[1] #根据标准请求头结构,解析出文件名,要先decode
    print(data.decode('utf-8'))
    conn.send('HTTP/1.1 200 OK \r\n\r\n'.encode('utf-8'))
    if request_file == '/': #开始判断请求的具体是哪个文件
        with open('test.html','rb') as f1:
            conn.send(f1.read())
    elif request_file == '/css1.css':
        with open('css1.css','rb') as f1:
            conn.send(f1.read())
    conn.close()
#这样的话 也可以添加 js文件 img图片文件的src链接等

并发的web框架

#把服务器返回文件分别写在函数中,并发的方式返回给请求端
import socket
from threading import Thread
sc = socket.socket()
ip_port=('127.0.0.1',8800)
sc.bind(ip_port)
sc.listen()

def html(conn):
    with open('test.html', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def css(conn):
    with open('css1.css', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def js(conn):
    with open('js1.js', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def jquery_js(conn):
    with open('jquery-3.5.1.js', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def png(conn):
    with open('1.png', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

request_list = [('/',html),('/css1.css',css),('/js1.js',js),('/jquery-3.5.1.js',jquery_js),('/1.png',png)] #罗列请求的内容

while 1:
    conn,addr=sc.accept()
    data = conn.recv(1024)    # data是请求头
    print(data.decode('utf-8'))
    conn.send(b'HTTP/1.1 200 ok\r\n\r\n')
    request_path = data.decode('utf-8').split('\r\n')[0].split()[1]    # 按照回车换行分割,获取请求的文件名
    print(request_path)

    for i in request_list:
        if request_path == i[0]:
            print(i[0])
            t = Thread(target=i[1],args=(conn,))  #target 是函数,args 是参数,对应上面多个函数
            t.start()

动态web框架

<!-- html 文件增加一个 <div>{time_now}</div> -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link rel="stylesheet" href="css1.css">
    <script src="jquery-3.5.1.js"></script>
    <script src="js1.js"></script>

</head>
<body>
    <div>{time_now}</div>  <!--模拟数据库,获取后台传入的当前时间-->

</body>
</html>
import socket
import time
from threading import Thread

sc = socket.socket()
ip_port=('127.0.0.1',8800)
sc.bind(ip_port)
sc.listen()

def html(conn):
    time_now = time.strftime('%Y/%m/%d %H:%M:%S',time.localtime()) #增加一个日期变量,传入到网页中
    with open('test.html', 'rb') as f:
        content = f.read().decode('utf-8').format(time_now=time_now) #content是个字符串,用一个format格式化一下内部的参数
        conn.send(content.encode('utf-8'))
        conn.close()

def css(conn):
    with open('css1.css', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def js(conn):
    with open('js1.js', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def jquery_js(conn):
    with open('jquery-3.5.1.js', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

def png(conn):
    with open('1.png', 'rb') as f:
        content = f.read()
        conn.send(content)
        conn.close()

request_list = [('/',html),('/css1.css',css),('/js1.js',js),('/jquery-3.5.1.js',jquery_js),('/1.png',png)] #罗列请求的内容

while 1:
    conn,addr=sc.accept()
    data = conn.recv(1024)    # data是请求头
    print(data.decode('utf-8'))
    conn.send(b'HTTP/1.1 200 ok\r\n\r\n')
    request_path = data.decode('utf-8').split('\r\n')[0].split()[1]    # 按照回车换行分割,获取请求的文件名
    print(request_path)

    for i in request_list:
        if request_path == i[0]:
            print(i[0])
            t = Thread(target=i[1],args=(conn,))  #target 是函数,args 是参数,对应上面多个函数
            t.start()

标签:socket,python,小渣,send,js,content,sc,Django,conn
来源: https://www.cnblogs.com/94xzz/p/14331358.html