如何在Python中创建一个简单的多线程套接字服务器以记住客户端
我想知道怎么用Python做一个简单的回声服务器,这个服务器能记住连接的客户端,而且每次请求时不需要新建一个连接。它还得能支持多个客户端同时访问。我希望能连接一次,然后持续发送和接收数据,像下面这个客户端一样:
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)
也就是说,服务器运行在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
1 个回答
100
你可以为每个客户端使用一个线程,这样就可以避免在 client.recv()
这个地方卡住。主线程只用来监听新客户端的连接。当有新的客户端连接时,主线程会创建一个新的线程来专门处理这个新客户端,等到它60秒没有发言后,这个线程就会结束。
import socket
import threading
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1024
while True:
try:
data = client.recv(size)
if data:
# Set the response to echo back the recieved data
response = data
client.send(response)
else:
raise error('Client disconnected')
except:
client.close()
return False
if __name__ == "__main__":
while True:
port_num = input("Port? ")
try:
port_num = int(port_num)
break
except ValueError:
pass
ThreadedServer('',port_num).listen()
如果客户端在60秒内没有任何活动,它会超时,需要重新连接。可以在 ThreadedServer.listen()
函数中找到 client.settimeout(60)
这一行。