If you're stuck on some Python version < 2.6
, one (imperfect but usable) approach is to do
import socket
socket.setdefaulttimeout(10.0) # or whatever
before you start using httplib
. The docs are here, and clearly state that setdefaulttimeout
is available since Python 2.3 -- every socket made from the time you do this call, to the time you call the same function again, will use that timeout of 10 seconds. You can use getdefaulttimeout before setting a new timeout, if you want to save the previous timeout (including none) so that you can restore it later (with another setdefaulttimeout
).
These functions and idioms are quite useful whenever you need to use some older higher-level library which uses Python socket
s but doesn't give you a good way to set timeouts (of course it's better to use updated higher-level libraries, e.g. the httplib
version that comes with 2.6 or the third-party httplib2
in this case, but that's not always feasible, and playing with the default timeout setting can be a good workaround).