Socket实现Web应用的本质

代码如下:

import socket

def handle_request(client):

    request_data = client.recv(1024)
    print("request_data: ",request_data)

    client.send("HTTP/1.1 200 OK
status: 200
Content-Type:text/html

".encode("utf8"))
    client.send("<h1>Hello, luffycity!</h1><img src='https://dwz.cn/5sq5CDaD'>".encode("utf8"))

def main():

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost',8812))
    sock.listen(5)

    while True:
        print("the server is waiting for client-connection....")
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':

    main()
原文地址:https://www.cnblogs.com/yigongzi/p/11646670.html