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 = select.select(self.sockets, [], [], 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) 
                self.clients.append(client)

                # Append the new socket to the client handlers list of sockets                
                self.sockets.append(c)

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

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

                    # Append the data to the clients queue
                    sender.appenddata(data)

                    # Handle any complete requests there may be
                    sender.handle()                    

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

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

                    # ... and remove the socket descriptor from the list of avilable sockets                                                                      
                    self.sockets.remove(s)

                    # Close the socket
                    s.close()                                                        
        # Return            
        return True