[Python-Dev] Adding timeout to socket.py and httplib.py

Alan Kennedy python-dev at alan.kennedy.name
Tue Mar 20 15:08:11 CET 2007


[Facundo Batista]
> Do you have any news about this?

Re: Patch 1676823
http://sourceforge.net/tracker/index.php?func=detail&aid=1676823&group_id=5470&atid=305470

Since I've just written a lot of socket stuff for jython, I thought
I'd have a look at the patch.

I like the idea of adding better socket control to the higher-level
modules like httplib, etc, because these modules don't provide access
to the underlying sockets, and using the socket module methods
setdefaulttimeout, etc, is a little messy.

I see that your updated socket.connect() method takes a timeout
parameter, which defaults to None if not present, e.g.

def connect(address, timeout=None):

Later in the function, this line appears

if timeout is not None:
  sock.settimeout(timeout)

The problem with this is that None has a meaning as a timeout value;
it means "put this socket in blocking mode". But that value can no
longer be used for socket connects, since that value is being
interpreted as "parameter was not provided".

So, if a non-standard timeout has been set, using something like

import socket ; socket.setdefaulttimeout(10.0)

how do I restore full blocking behaviour to a single socket? (a
somewhat contrived case, I admit).

If I have access to the socket object, then I can call
"sock_obj.settimeout(None)", but in that case I don't need the new
API. I could also do it with the call "sock_obj.setblocking(1)".

If I don't have access to the socket object, i.e. I'm using timeouts
indirectly through httplib/etc, then I'm stuck: there's no way I can
change the blocking or timeout behaviour; back to square one.

So the new proposed API partly addresses the problem of increasing
control over the underlying socket, but doesn't address all cases. It
specifically prevents setting a timeout value of None on a socket,
which is an important use case, I think.

Regards,

Alan.


More information about the Python-Dev mailing list