首先写一段代码,然后运行。
import socket #生成socket实例对象 sk = socket.socket() #绑定IP和端口 sk.bind(("127.0.0.1",8001)) #监听 sk.listen() while 1: #获取与客户端的连接 conn, _ = sk.accept() #接收客户商发来消息 data = conn.recv(8096) print(data) #给客户端回复消息 conn.send(b'CNBB') conn.close() sk.close()
在浏览器中返回结果:
将上面的代码copy出来:
GET / HTTP/1.1 Host: 127.0.0.1:8001\ User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 irefox/61.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 Accept-Encoding: gzip, deflate Cookie: csrftoken=dijVYxB5kwFxxyu77ywTF4w4R7t2XmAwdvxch8byLyCVaB4YZM6PpPHTsdUmRxZ1 Connection: keep-alive Upgrade-Insecure-Requests: 1'