浏览器是客户端,写一段代码作为服务端,在浏览器的输入框写入ip和端口号,就可以通信。
浏览器向服务器发出请求,服务器将网页发件发送给浏览器,浏览器解析,渲染收到的文件
sk=socket.socket() address=('127.0.0.1',9001) sk.bind(address) sk.listen(3) while 1: con,adr=sk.accept() a=con.recv(1024) a=a.decode('utf8') con.sendall(bytes("HTTP/1.1 201 OK\r\n\r\n","utf8")) with open('moni.html','rb') as f: data=f.read() con.sendall(data) 服务器将网页文件发送给浏览器,浏览器进行解析。 con.close()
以上代码 中,a的内容是服务器接受到的浏览器发出的请求内容。
大致为:
GET / HTTP/1.1
Host: 127.0.0.1:9001
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9