如何创建一个简单的Python echo服务器来记住客户端,而不是为每个请求创建一个新的套接字?必须能够支持并发访问.我希望能够连接一次并使用此客户端或类似程序不断发送和接收数据:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = raw_input("Server hostname or ip? ")
port = input("Server port? ")
sock.connect((host,port))
while True:
data = raw_input("message: ")
sock.send(data)
print "response: ", sock.recv(1024)
Run Code Online (Sandbox Code Playgroud)
即使用在端口50000上运行的服务器,使用上面的客户端我希望能够这样做:
me@mine:~$ client.py
Server hostname or ip? localhost
Server Port? 50000
message: testa
response: testa
message: testb
response: testb
message: testc
response: testc
Run Code Online (Sandbox Code Playgroud)