the nature of the request server python

Different routing request different content

'''
Return different content according to the different paths the url
'''
import socket
sk = socket.socket, (),
sk.bind (( " 127.0.0.1 " , 8080)) # bind ip port 
sk.listen () # monitor

the while . 1 :
     # wait for a connection 
    Conn, the Add = sk.accept ()
    data = conn.recv (8096) # receiving the message sent by the client 
    # taken from data path 
    data = STR (data, encoding = " UTF8 " ) # the received byte type turn into a string 
    # press \ r \ n divided 
    DATAl = data.split ( " \ r \ n " ) [0]
    URL = data1.split () [. 1] # URL we sent a message from the browser to access paths separated 
    conn.send (B ' the HTTP / 1.1 200 is the OK \ R & lt \ n-\ R & lt \ n- ' ) # because to respect the Asahi http protocol, so the reply message should also increase the status line 
    # according to different paths, different content to return 
    IF url == " / index / " :
        response=b"index"
    elif url=="/home/" :
        response=b"home"
    else:
        response=b"404 not found"

    conn.send(response)
    conn.close()

 

Guess you like

Origin www.cnblogs.com/huay/p/11095114.html