Subclassing socket

groups.20.thebriguy at groups.20.thebriguy at
Wed Dec 21 04:42:31 CET 2005

socket objects have a little quirk.  If you try to receive 0 bytes on a
blocking socket, they block.  That is, if I call recv(0), it blocks
(until some data arrives).

I think that's wrong, but I don't want to argue that.  I would like to
create a subclass of socket that fixes the problem.  Ideally, something

class new_socket(socket):
    def recv( self, bufsize, flags=0 ):
        if bufsize == 0:
            return ""
            return socket.recv( bufsize, flags )

They only problem is, sockets return socket objects via the accept
call.  And the socket returned is of type socket, of course, not
new_socket, as I would like.  I could override accept() to return a
new_socket, but I don't know how to convert the old socket to a new
socket.  That is, I'd like to add a method to the class above something

    def accept( self ):
        conn, addr = socket.accept()
        <convert conn, which is type socket to type new_socket>
        return ( conn, addr )

Does anyone have any suggestions on how to do the above?

More information about the Python-list mailing list