There's always try
, which is pretty much perfect for this sort of thing:
import socket
import urllib2
try:
handle = urllib2.urlopen(req, timeout = timeout)
except TypeError:
socket.setdefaulttimeout(timeout)
handle = urllib2.urlopen(req)
(Make sure to use the keyword argument for this in the first call, otherwise python 2.5 will think you are passing it the 'data' argument.)