with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s: s.bind(("0.0.0.0",1234)) s.listen() whileTrue: c, addr = s.accept() t = threading.Thread(target=handle_http_client, args=(c,addr)) t.start()
1 2 3 4 5 6 7 8
# socket客户端 这里未实现http客户端(直接用浏览器代替) import socket
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s: s.connect(("127.0.0.1",1234)) s.sendall(b"Hello,Xiaoming!") data = s.recv(1024) print("Received:",repr(data))