Multi Threaded TCP server in Python

前端 未结 2 683
盖世英雄少女心
盖世英雄少女心 2020-12-05 07:32

I have created a simple multi threaded tcp server using python\'s threding module. This server creates a new thread each time a new client is connected.

#!/u         


        
相关标签:
2条回答
  • 2020-12-05 08:28

    I have created this nice class you can override

    import socket
    import thread
    
    class SocketServer(socket.socket):
        clients = []
    
        def __init__(self):
            socket.socket.__init__(self)
            #To silence- address occupied!!
            self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.bind(('0.0.0.0', 8080))
            self.listen(5)
    
        def run(self):
            print "Server started"
            try:
                self.accept_clients()
            except Exception as ex:
                print ex
            finally:
                print "Server closed"
                for client in self.clients:
                    client.close()
                self.close()
    
        def accept_clients(self):
            while 1:
                (clientsocket, address) = self.accept()
                #Adding client to clients list
                self.clients.append(clientsocket)
                #Client Connected
                self.onopen(clientsocket)
                #Receiving data from client
                thread.start_new_thread(self.recieve, (clientsocket,))
    
        def recieve(self, client):
            while 1:
                data = client.recv(1024)
                if data == '':
                    break
                #Message Received
                self.onmessage(client, data)
            #Removing client from clients list
            self.clients.remove(client)
            #Client Disconnected
            self.onclose(client)
            #Closing connection with client
            client.close()
            #Closing thread
            thread.exit()
            print self.clients
    
        def broadcast(self, message):
            #Sending message to all clients
            for client in self.clients:
                client.send(message)
    
        def onopen(self, client):
            pass
    
        def onmessage(self, client, message):
            pass
    
        def onclose(self, client):
            pass
    

    And here's an example:

    class BasicChatServer(SocketServer):
    
        def __init__(self):
            SocketServer.__init__(self)
    
        def onmessage(self, client, message):
            print "Client Sent Message"
            #Sending message to all clients
            self.broadcast(message)
    
        def onopen(self, client):
            print "Client Connected"
    
        def onclose(self, client):
            print "Client Disconnected"
    
    def main():
        server = BasicChatServer()
        server.run()
    
    if __name__ == "__main__":
        main()
    
    0 讨论(0)
  • 2020-12-05 08:29

    You should pass the client sock to the thread like you do with the ip address and the port:

    class ClientThread(threading.Thread):
    
        def __init__(self, ip, port, socket):
            threading.Thread.__init__(self)
            self.ip = ip
            self.port = port
            self.socket = socket
            print "[+] New thread started for "+ip+":"+str(port)
    
        def run(self):
            # use self.socket to send/receive
    
    ...
    (clientsock, (ip, port)) = tcpsock.accept()
    newthread = ClientThread(ip, port, clientsock)
    ...
    
    0 讨论(0)
提交回复
热议问题