Python Socket

Details to come.
Here’s a short description in the meanwhile:

This is a class I wrote for a host, which should handle some requests, given by clients.
The part to notice here is the way select() is used. I found that socket.recv() was a blocking call and since any method of trying to make it non-blocking failed with some error, I had to use select(). Select reads from selectable inputs (ie. socket or user inputs), and returns a list of sockets with data read. You may then use socket.recv() on these sockets, because there is data avaiable and this won’t block the program while waiting for data.

class ClientHandler():
    # Initializes a client handler
    # Takes the host socket as argument
    def __init__(self, socket):
        self.hostsoc = socket
        self.sockets = [self.hostsoc]       
        self.clients = []                 

    # Automatically removes dead clients
    def serveclients(self):

        # Select available sockets
        rready, wready, err =, [], [], 0)
        for s in rready:

            # If there was a new connection
            if s == self.hostsoc:                                                                                                                                                                                                           
                # Accept the new socket
                c, addr = s.accept()
                # Create new clients of the new socket
                client = Client(c, addr) 

                # Append the new socket to the client handlers list of sockets                

            # If data was available on existing clients                                                                                                                      
                data = s.recv(1024)

                if data:
                    # Get the sender (client)
                    for c in self.clients:
                        if s == c.socket():
                            sender = c

                    # Append the data to the clients queue

                    # Handle any complete requests there may be

                    # Add data to client data queue
                    #print "Received %s" % ( data )

                    # Mark the corresponding client as inactive ...
                    for c in self.clients:
                        if s == c.socket():

                    # ... and remove the socket descriptor from the list of avilable sockets                                                                      

                    # Close the socket
        # Return            
        return True