How to make a simple multithreaded socket server in Python that remembers clients

前端 未结 1 867
迷失自我
迷失自我 2020-12-07 13:44

How do I make a simple Python echo server that remembers clients and doesn\'t create a new socket for each request? Must be able to support concurrent access. I want to be a

相关标签:
1条回答
  • 2020-12-07 14:13

    You can use a thread per client to avoid the blocking client.recv() then use the main thread just for listening for new clients. When one connects, the main thread creates a new thread that just listens to the new client and ends when it doesn't talk for 60 seconds.

    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()
    

    Clients timeout after 60 seconds of inactivity and must reconnect. See the line client.settimeout(60) in the function ThreadedServer.listen()

    0 讨论(0)
提交回复
热议问题