kennethreitz/requests — Issue #290: timeout not working?
Author: cactus
Created: 2011-11-26 11:46:59+00:00
State: open

It seems that timeouts are not working when the host cannot be reached. It appears that the standard socket timeout timeframe is being used (I get similar timings with timeouts via telnet to the same non listening host/port for example).

~~~ .python
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> import requests
>>> import time
>>> def callwithtime(callable, *args, **kwds):
... t1 = time.time()
... try:
... callable(*args, **kwds)
... except:
... pass
... t2 = time.time()
... print '%s took %0.3f ms' % (callable.func_name, (t2-t1)*1000.0)
...
>>> callwithtime(requests.get, 'http://1.1.1.1/foo.txt')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 1.1.1.1
^Cget took 10215.639 ms
>>> callwithtime(requests.get, 'http://1.1.1.1/foo.txt', timeout=1)
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 1.1.1.1
WARNING:requests.packages.urllib3.connectionpool:Retrying (0 attempts remain) after connection broken by 'error(60, 'Operation timed out')': /foo.txt
get took 75865.529 ms
>>> callwithtime(requests.get, 'http://1.1.1.1/foo.txt', timeout=0.1)
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 1.1.1.1
WARNING:requests.packages.urllib3.connectionpool:Retrying (0 attempts remain) after connection broken by 'error(60, 'Operation timed out')': /foo.txt
get took 75441.546 ms
~~~