The path to learning in Python -2018/6/14 1. Browser and server
The process by which the browser sends a request to the server is the process of requesting, and the server responds to the browser as a response process.
2. A simple Web application
ImportSocketsock=Socket.socket () Sock.bind ("100.113.14.43",8080)) Sock.listen (5) with Open("Index.html","R") asF:# First write the style to the HTML file, and then read the Send to the browserContent=F.read () while True:Print("Server starting ...") Conn, addr=Sock.accept () data=CONN.RECV (1024x768)Print("Data:", data) Conn.send (("http/1.1 OK\r\n\r\n%s" %Content). Encode ("UTF8"))# HTTP protocol formatConn.close () Sock.close ()
Index.html
<! DOCTYPE html> lang= Span class= "st" > "en" > <HEAD> charset= > span class= "kw" ><TITLE> title</TITLE> <BODY> <H1> Hello world</ H1> src= > </BODY> </HTML>
Next, use your browser to access 100.113.14.43:8080
3. Request Agreement
Request format
Get vs. Post differences
Response format
Response Status Code
|
type |
cause |
1XX |
Information (information) |
The request is being processed |
2XX |
Success (Success) |
Request Processing completed |
3XX |
Redirection (redirected) |
Additional action required to complete the request |
4XX |
Client error (Clients errors) |
The server cannot process the request |
5XX |
Server error (server errors) |
Server Processing Request Error |
fromWsgiref.simple_serverImportMake_serverdefAppliaction (environ, start_response):# Environ: Parsing data by HTTP protocol # Strat_response: Assemble data according to HTTP protocolPath=Environ.get ("Path_info") Start_response ("OK", []) with Open("Index.html","RB") asF:data1=F.read () with Open("Login.html","RB") asF:data2=F.read ()ifPath== "/index":return[Data1]elifPath== "/login":return[Data2]Else:return[b"]httpd=Make_server ("",8090, appliaction)Print("Server starting..")# Start listeningHttpd.serve_forever ()
The path to learning in Python -2018/6/14