Is there a way to globally override requests' timeout setting?
The simplest way is to "shim" the session's request
function:
import requests
import functools
s = requests.Session()
s.request = functools.partial(s.request, timeout=3)
# now all get, post, head etc requests should timeout after 3 seconds
# following will fail
s.get('https://httpbin.org/delay/6')
# we can still pass higher timeout when needed
# following will succeed
s.get('https://httpbin.org/delay/6', timeout=7)
Instead you could inherit the requests.Session class and rewrite request
function, like this.
HTTP_TIMEOUT = 30
class TimeoutRequestsSession(requests.Session):
def request(self, *args, **kwargs):
kwargs.setdefault('timeout', HTTP_TIMEOUT)
return super(TimeoutRequestsSession, self).request(*args, **kwargs)
session = TimeoutRequestsSession()
session.get('https://www.google.com') # connection timeout is default 30s
Unfortunately, looking at the code, there is no possibility to set a global default value. I was kinda surprised by that, as I would expect that to be quite common use case. If you start a feature request, please let me know (e.g. in comments to this post).
The reason for that is that methods like get(...)
, post(...)
, etc are all just thin wrappers over Session.request(...)
method (requests.get(...)
creates new one-shot session, just for a single request). That method takes timeout
as argument, and does not inspect Session
internals for a value if there is no timeout argument, so you always have to put it there manually, like 2ps proposed in his answer.
Sources:
Revised on master on 31.08.2020. Line numbers have changed, but methods stayed the same. The answer stays the same.
requests/__init__.py
- import API to package scope, to providerequests.get(...)
-like utilitiesrequests.api
- API module that is imported in point above; usess one-shot sessionsrequests.sessions
-Session
implementation- line 337 starts
Session
class - line 463 starts
request(...)
method - line 526 actually uses
timeout
parameter - line 534 start
get(...)
method
- line 337 starts
PS. See this pull request. Disclaimer: it's mine.