From a180db963f08ab1a3b1fd036b3cd5f6bae791a5a Mon Sep 17 00:00:00 2001 From: Alexander 'Leo' Bergolth Date: Wed, 29 Mar 2017 16:53:28 +0200 Subject: [PATCH] allow urllib3 Timeout objects as timeout parameter for HTTPAdapter.send() omit timeout argument when calling urlopen on the connection. this allows setting a default timeout at connection pool level: adapter.poolmanager.connection_pool_kw['timeout'] = urllib3.Timeout(...) --- requests/adapters.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index 373fe5d0..529dfd00 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -391,7 +391,7 @@ class HTTPAdapter(BaseAdapter): :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) ` tuple. - :type timeout: float or tuple + :type timeout: float or tuple or urllib3 Timeout object :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use @@ -408,7 +408,10 @@ class HTTPAdapter(BaseAdapter): chunked = not (request.body is None or 'Content-Length' in request.headers) - if isinstance(timeout, tuple): + + if isinstance(timeout, TimeoutSauce): + pass + elif isinstance(timeout, tuple): try: connect, read = timeout timeout = TimeoutSauce(connect=connect, read=read) @@ -418,11 +421,14 @@ class HTTPAdapter(BaseAdapter): "timeout tuple, or a single float to set " "both timeouts to the same value".format(timeout)) raise ValueError(err) - else: + elif timeout is not None: timeout = TimeoutSauce(connect=timeout, read=timeout) try: if not chunked: + kwargs = {} + if timeout is not None: + kwargs['timeout'] = timeout resp = conn.urlopen( method=request.method, url=url, @@ -433,7 +439,7 @@ class HTTPAdapter(BaseAdapter): preload_content=False, decode_content=False, retries=self.max_retries, - timeout=timeout + **kwargs ) # Send the request.