According to the socket we learned before, we can understand that all Web applications are essentially a socket server, and the user's browser is a socket client. This allows us to implement the web framework ourselves.
1. Custom Web framework for semi-finished products
Import socket
SK = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Sk.bind (("127.0.0.1", 80))
Sk.listen ()
While 1:
conn, addr = Sk.accept () # Establish connection
data = Conn.recv (8096) # Send and receive messages
Print (data) # Prints the message from the browser
Conn.send (b "Welcome") # Reply message
Conn.close ()
User's browser input URL, will send data to the server, what data will the browser send? How to send? Who's going to fix this?
At this time there is a unified rule, let everyone send messages, receive messages when there is a format basis, not casually write. This rule is the HTTP protocol, after the browser sends the request information, the server replies to the response information, should follow this rule.
The HTTP protocol mainly specifies the communication format between the client and the server, how does the HTTP protocol specify the message format? Look at the data we printed out.
B ' get/http/1.1\r\nhost:127.0.0.1:8080\r\nconnection:keep-alive\r\npragma:no-cache\r\ncache-control:no-cache\r\ nupgrade-insecure-requests:1\r\nuser-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.181 Safari/537.36\r\naccept:text/html,ap Plication/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\naccept-encoding:gzip, deflate, br\r \naccept-language:zh-cn,zh;q=0.9,en;q=0.8\r\n\r\n '
2. XXX Version Custom Web frame
Import socket
Server=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
Server.bind (("127.0.0.1", 8080))
Server.listen (5)
While 1:
Conn,addr=server.accept ()
DATA=CONN.RECV (1024)
Print (data)
Conn.send (b "http/1.1 ok\r\n\r\n") # Add response status line to reply message
Conn.send (b "Welcome")
Conn.close ()
3. Implement different paths to return different content
3.1 The low version
Import socket
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Server.bind (("127.0.0.1", 8082))
Server.listen (5)
While 1:
CONN,ADDR = Server.accept ()
data = CONN.RECV (1024)
Data_str = str (data,encoding= "Utf-8")
url = data_str.split ("\ r \ n") [0].split () [1]
If url = = '/index ':
msg = B "This is index HTML"
Elif url = = '/home ':
msg = B "This is home HTML"
Else
msg = b "404 Not Found!!!"
Conn.send (b "http/1.1 ok\r\n\r\n")
Conn.send (msg)
Conn.close ()
3.2 Function Edition
Import socket
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Server.bind (("127.0.0.1", 8082))
Server.listen (5)
def index (URL):
Return B "This is index HTML"
def home (URL):
Return B "This is home HTML"
While 1:
CONN,ADDR = Server.accept ()
data = CONN.RECV (1024)
Data_str = str (data,encoding= "Utf-8")
url = data_str.split ("\ r \ n") [0].split () [1]
If url = = '/index ':
msg = index (URL)
Elif url = = '/home ':
msg = Home (URL)
Else
msg = b "404 Not Found!!!"
Conn.send (b "http/1.1 ok\r\n\r\n")
Conn.send (msg)
Conn.close ()
3.3 Function Step
Import socket
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Server.bind (("127.0.0.1", 8082))
Server.listen (5)
def index (URL):
Return B "This is index HTML"
def home (URL):
Return B "This is home HTML"
Url_list = [
("/index", Index),
("Home",
] # defines the correspondence between a URL and the actual function to be executed
While 1:
CONN,ADDR = Server.accept ()
data = CONN.RECV (1024)
Data_str = str (data,encoding= "Utf-8")
url = data_str.split ("\ r \ n") [0].split () [1]
func = None # Defines a variable that holds the name of the function that will be executed
For I in Url_list:
If i[0] = = URL:
Func = i[1]
Break
If Func:
msg = func (URL)
Else
msg = b "404 Not Found!"
Conn.send (b "http/1.1 ok\r\n\r\n")
Conn.send (msg)
Conn.close ()
Web Framework Nature