import socket
# 监听本机的IP和端口
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('10.10.10.170', 9000))
sock.listen(10)
while True:
# 3.等待连接请求的申请,有人来连接(阻塞) -> 登录浏览器来连接我
conn, addr = sock.accept()
# 4.收到浏览器发送的消息
data = conn.recv(2048).decode('utf-8')
print(data)
# 5.给浏览器返回数据
conn.send(b"HTTP/1.1 200 OK\r\n\r\n")
conn.send(b"Hello, World")
conn.close()
# 6.停止服务端程序
sock.close()
运行