tags:

views:

30

answers:

2

I'm trying to create my own subclass of socket.socket that will be able to handle custom messages. So far my code looks like this:

    self._sockets.append(s)
    logging.debug("Waiting for incoming connections on port %d" % (port))
    while not self.shutdown:
        inputready,outputready,exceptready = select(self._sockets,[],[]) 
        print "Select returned"
        for i in inputready: 
            if s == i:
                # handle the server socket 
                client, address = s.accept()
                self._sockets.append(client)
                print "%r , %r" % (client, address) 
            else: 
                # handle all other sockets 
                s.handleMessage() 

so as you can see I'm either acceptin new connections or if it returned from another socket it'll call handleMessage on that socket. Now the problem is that off course socket.accept() will return a socket.socket and not my subclass which implements the handleMessage function.

What would be the easiest way to get my custom class instead of the default socket.socket?

+1  A: 

How difficult is it to setup a dictionary mapping socket descriptors to your socket wrapper objects?

Nikolai N Fetissov
+2  A: 

From your description it appears that you are making a message handler that has-a socket (or sockets). When designing classes has-a indicates composition and delegation while is-a can indicate inheritance.

So it is not appropriate to inherit from socket.socket, and your code is already looking a bit hybrid. Something like this really coarse pseudo-code is probably best suited to the task:

class MyMessageHandler(object):
    def __init__(self):
        self.sockets = [...]

    def wait(self):
        debug('waiting...')
        i, o, e = select(...)
msw
+1: Delegation makes more sense here than inheritance.
S.Lott