python thread exception

sandorlev 0 Tallied Votes 458 Views Share

Hello! I started to create my own tiny 'framework' mainly built on the socket and the thread module. I haven't been doing too much thread programming before. My problem is that when I try server.waitForConnections(2), at the second connection, it says

Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:

What should I do to make it work?

Edit: by 'at the second connection' I mean, after the connection has been established, exactly

class SLServerSocket(socket.socket):

    def __init__(self, ...):
        ...

    def waitForClients(self, x, function=None, *args, **kwargs):
        """Waits for x clients to connect, then if all of them
        has closed connection, closes the server.

        """
        clientsJoined = 0
        while clientsJoined < x:
            clientSocket, address = self.accept()
            self._clients.append(clientSocket)
            clientsJoined += 1
            thread.start_new_thread(self._handleConnection,
                            (clientSocket, function, args, kwargs))
        self._shutDown()

    def _handleConnection(self, clientSocket, function, *args, **kwargs):
        if function is None:
            echo(clientSocket)
        else:
            #clientSocket is always passed to the function
            function(clientSocket, args, kwargs)
        self._closeConnection(clientSocket)

def echo(clientSocket):
    while True:
        data = clientSocket.recv(BUFFSIZE)
        if not data:
            break
        else:
            clientSocket.send(data)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.