Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

use settings.timeout_fallback bool for socket.setdefaulttimeout() fal…

…lback
  • Loading branch information...
commit 88f79b74ce70815b343f5eeff871f0ee9e8cec67 1 parent 647cdd3
Jens Diemer authored

Showing 2 changed files with 12 additions and 5 deletions. Show diff stats Hide diff stats

  1. +1 0  requests/config.py
  2. +11 5 requests/models.py
1  requests/config.py
@@ -57,3 +57,4 @@ def __getattribute__(self, key):
57 57 settings.base_headers = {'User-Agent': 'python-requests.org'}
58 58 settings.accept_gzip = True
59 59 settings.proxies = None
  60 +settings.timeout_fallback = True # Use socket.setdefaulttimeout() as fallback?
16 requests/models.py
@@ -38,7 +38,7 @@ def __init__(self,
38 38 params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False,
39 39 allow_redirects=False, proxies=None):
40 40
41   - #socket.setdefaulttimeout(timeout)
  41 + #: Float describ the timeout of the request. (Use socket.setdefaulttimeout() as fallback)
42 42 self.timeout = timeout
43 43
44 44 #: Request URL.
@@ -313,11 +313,17 @@ def send(self, anyway=False):
313 313 # timeout argument is new since Python v2.6
314 314 if not "timeout" in str(err):
315 315 raise
316   - # set global socket timeout
317   - old_timeout = socket.getdefaulttimeout()
318   - socket.setdefaulttimeout(self.timeout)
  316 +
  317 + if settings.timeout_fallback:
  318 + # fall-back and use global socket timeout (This is not thread-safe!)
  319 + old_timeout = socket.getdefaulttimeout()
  320 + socket.setdefaulttimeout(self.timeout)
  321 +
319 322 resp = opener(req)
320   - socket.setdefaulttimeout(old_timeout)
  323 +
  324 + if settings.timeout_fallback:
  325 + # restore gobal timeout
  326 + socket.setdefaulttimeout(old_timeout)
321 327
322 328 if self.cookiejar is not None:
323 329 self.cookiejar.extract_cookies(resp, req)

0 comments on commit 88f79b7

Please sign in to comment.
Something went wrong with that request. Please try again.