首页 > 代码库 > http的请求流程

http的请求流程

 1 # !/usr/bin/env python
 2 # coding:utf-8
 3 import socket
 4 
 5 
 6 def handle_request(client):
 7     buf = client.recv(1024)
 8     client.send("HTTP/1.1 200 OK\r\n\r\n")#状态码
    #client.send("Content-Type:text/html\r\n\r\n")#页面显示的格式
9 client.send("<a href=http://www.mamicode.com/‘http://www.baidu.com‘>Hello, World") 10 11 12 def main(): 13 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 14 sock.bind((localhost, 8080)) 15 sock.listen(5) 16 17 while True: 18 connection, address = sock.accept() 19 handle_request(connection)#当服务器接受请求并返回数据之后就断开连接不会出现交互的模式 20 connection.close() 21 22 23 if __name__ == __main__: 24 main()

 

http的请求流程