1515from .packages .urllib3 .poolmanager import PoolManager , proxy_from_url
1616from .packages .urllib3 .response import HTTPResponse
1717from .packages .urllib3 .util import Timeout as TimeoutSauce
18- from .compat import urlparse , basestring , urldefrag , unquote
18+ from .compat import urlparse , basestring
1919from .utils import (DEFAULT_CA_BUNDLE_PATH , get_encoding_from_headers ,
20- prepend_scheme_if_needed , get_auth_from_url )
20+ prepend_scheme_if_needed , get_auth_from_url , urldefragauth )
2121from .structures import CaseInsensitiveDict
22- from .packages .urllib3 .exceptions import MaxRetryError
23- from .packages .urllib3 .exceptions import TimeoutError
24- from .packages .urllib3 .exceptions import SSLError as _SSLError
22+ from .packages .urllib3 .exceptions import ConnectTimeoutError
2523from .packages .urllib3 .exceptions import HTTPError as _HTTPError
24+ from .packages .urllib3 .exceptions import MaxRetryError
2625from .packages .urllib3 .exceptions import ProxyError as _ProxyError
26+ from .packages .urllib3 .exceptions import ProtocolError
27+ from .packages .urllib3 .exceptions import ReadTimeoutError
28+ from .packages .urllib3 .exceptions import SSLError as _SSLError
2729from .cookies import extract_cookies_to_jar
28- from .exceptions import ConnectionError , Timeout , SSLError , ProxyError
30+ from .exceptions import (ConnectionError , ConnectTimeout , ReadTimeout , SSLError ,
31+ ProxyError )
2932from .auth import _basic_auth_str
3033
3134DEFAULT_POOLBLOCK = False
@@ -267,7 +270,7 @@ def request_url(self, request, proxies):
267270 proxy = proxies .get (scheme )
268271
269272 if proxy and scheme != 'https' :
270- url , _ = urldefrag (request .url )
273+ url = urldefragauth (request .url )
271274 else :
272275 url = request .path_url
273276
@@ -314,7 +317,10 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
314317
315318 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
316319 :param stream: (optional) Whether to stream the request content.
317- :param timeout: (optional) The timeout on the request.
320+ :param timeout: (optional) How long to wait for the server to send
321+ data before giving up, as a float, or a (`connect timeout, read
322+ timeout <user/advanced.html#timeouts>`_) tuple.
323+ :type timeout: float or tuple
318324 :param verify: (optional) Whether to verify SSL certificates.
319325 :param cert: (optional) Any user-provided SSL certificate to be trusted.
320326 :param proxies: (optional) The proxies dictionary to apply to the request.
@@ -328,7 +334,18 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
328334
329335 chunked = not (request .body is None or 'Content-Length' in request .headers )
330336
331- timeout = TimeoutSauce (connect = timeout , read = timeout )
337+ if isinstance (timeout , tuple ):
338+ try :
339+ connect , read = timeout
340+ timeout = TimeoutSauce (connect = connect , read = read )
341+ except ValueError as e :
342+ # this may raise a string formatting error.
343+ err = ("Invalid timeout {0}. Pass a (connect, read) "
344+ "timeout tuple, or a single float to set "
345+ "both timeouts to the same value" .format (timeout ))
346+ raise ValueError (err )
347+ else :
348+ timeout = TimeoutSauce (connect = timeout , read = timeout )
332349
333350 try :
334351 if not chunked :
@@ -386,10 +403,13 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
386403 # All is well, return the connection to the pool.
387404 conn ._put_conn (low_conn )
388405
389- except socket .error as sockerr :
390- raise ConnectionError (sockerr , request = request )
406+ except ( ProtocolError , socket .error ) as err :
407+ raise ConnectionError (err , request = request )
391408
392409 except MaxRetryError as e :
410+ if isinstance (e .reason , ConnectTimeoutError ):
411+ raise ConnectTimeout (e , request = request )
412+
393413 raise ConnectionError (e , request = request )
394414
395415 except _ProxyError as e :
@@ -398,8 +418,8 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
398418 except (_SSLError , _HTTPError ) as e :
399419 if isinstance (e , _SSLError ):
400420 raise SSLError (e , request = request )
401- elif isinstance (e , TimeoutError ):
402- raise Timeout (e , request = request )
421+ elif isinstance (e , ReadTimeoutError ):
422+ raise ReadTimeout (e , request = request )
403423 else :
404424 raise
405425
0 commit comments