浏览器请求,然后socket服务端通过open方法打开一个html文本,并发送给浏览器,代码如下:
import socket
def custom_html():
sk = socket.socket()
sk.bind(('127.0.0.1', 8008))
sk.listen(3)
while True:
con, add = sk.accept()
data = con.recv(1024)
print(data.decode('utf8'))
with open('hello.html', 'rb') as f:
html_data = f.read()
con.sendall(bytes('HTTP/1.1 201 OK\r\n\r\n', 'utf8'))
con.sendall(html_data)
con.close()
if __name__ == '__main__':
custom_html()
html代码如下:
<h1 style="color: aquamarine">hello,huangjin</h1>
在edge浏览器下,输入127.0.0.1:8008,回车,可以正常访问:
