annotate requests/adapters.py @ 11:7fd0ef5842e7

planemo upload for repository https://toolrepo.galaxytrakr.org/view/jpayne/bioproject_to_srr_2/556cac4fb538
author jpayne
date Mon, 06 May 2024 01:42:27 -0400
parents 5eb2d5e3bf22
children
rev   line source
jpayne@7 1 """
jpayne@7 2 requests.adapters
jpayne@7 3 ~~~~~~~~~~~~~~~~~
jpayne@7 4
jpayne@7 5 This module contains the transport adapters that Requests uses to define
jpayne@7 6 and maintain connections.
jpayne@7 7 """
jpayne@7 8
jpayne@7 9 import os.path
jpayne@7 10 import socket # noqa: F401
jpayne@7 11
jpayne@7 12 from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
jpayne@7 13 from urllib3.exceptions import HTTPError as _HTTPError
jpayne@7 14 from urllib3.exceptions import InvalidHeader as _InvalidHeader
jpayne@7 15 from urllib3.exceptions import (
jpayne@7 16 LocationValueError,
jpayne@7 17 MaxRetryError,
jpayne@7 18 NewConnectionError,
jpayne@7 19 ProtocolError,
jpayne@7 20 )
jpayne@7 21 from urllib3.exceptions import ProxyError as _ProxyError
jpayne@7 22 from urllib3.exceptions import ReadTimeoutError, ResponseError
jpayne@7 23 from urllib3.exceptions import SSLError as _SSLError
jpayne@7 24 from urllib3.poolmanager import PoolManager, proxy_from_url
jpayne@7 25 from urllib3.util import Timeout as TimeoutSauce
jpayne@7 26 from urllib3.util import parse_url
jpayne@7 27 from urllib3.util.retry import Retry
jpayne@7 28
jpayne@7 29 from .auth import _basic_auth_str
jpayne@7 30 from .compat import basestring, urlparse
jpayne@7 31 from .cookies import extract_cookies_to_jar
jpayne@7 32 from .exceptions import (
jpayne@7 33 ConnectionError,
jpayne@7 34 ConnectTimeout,
jpayne@7 35 InvalidHeader,
jpayne@7 36 InvalidProxyURL,
jpayne@7 37 InvalidSchema,
jpayne@7 38 InvalidURL,
jpayne@7 39 ProxyError,
jpayne@7 40 ReadTimeout,
jpayne@7 41 RetryError,
jpayne@7 42 SSLError,
jpayne@7 43 )
jpayne@7 44 from .models import Response
jpayne@7 45 from .structures import CaseInsensitiveDict
jpayne@7 46 from .utils import (
jpayne@7 47 DEFAULT_CA_BUNDLE_PATH,
jpayne@7 48 extract_zipped_paths,
jpayne@7 49 get_auth_from_url,
jpayne@7 50 get_encoding_from_headers,
jpayne@7 51 prepend_scheme_if_needed,
jpayne@7 52 select_proxy,
jpayne@7 53 urldefragauth,
jpayne@7 54 )
jpayne@7 55
jpayne@7 56 try:
jpayne@7 57 from urllib3.contrib.socks import SOCKSProxyManager
jpayne@7 58 except ImportError:
jpayne@7 59
jpayne@7 60 def SOCKSProxyManager(*args, **kwargs):
jpayne@7 61 raise InvalidSchema("Missing dependencies for SOCKS support.")
jpayne@7 62
jpayne@7 63
jpayne@7 64 DEFAULT_POOLBLOCK = False
jpayne@7 65 DEFAULT_POOLSIZE = 10
jpayne@7 66 DEFAULT_RETRIES = 0
jpayne@7 67 DEFAULT_POOL_TIMEOUT = None
jpayne@7 68
jpayne@7 69
jpayne@7 70 class BaseAdapter:
jpayne@7 71 """The Base Transport Adapter"""
jpayne@7 72
jpayne@7 73 def __init__(self):
jpayne@7 74 super().__init__()
jpayne@7 75
jpayne@7 76 def send(
jpayne@7 77 self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
jpayne@7 78 ):
jpayne@7 79 """Sends PreparedRequest object. Returns Response object.
jpayne@7 80
jpayne@7 81 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
jpayne@7 82 :param stream: (optional) Whether to stream the request content.
jpayne@7 83 :param timeout: (optional) How long to wait for the server to send
jpayne@7 84 data before giving up, as a float, or a :ref:`(connect timeout,
jpayne@7 85 read timeout) <timeouts>` tuple.
jpayne@7 86 :type timeout: float or tuple
jpayne@7 87 :param verify: (optional) Either a boolean, in which case it controls whether we verify
jpayne@7 88 the server's TLS certificate, or a string, in which case it must be a path
jpayne@7 89 to a CA bundle to use
jpayne@7 90 :param cert: (optional) Any user-provided SSL certificate to be trusted.
jpayne@7 91 :param proxies: (optional) The proxies dictionary to apply to the request.
jpayne@7 92 """
jpayne@7 93 raise NotImplementedError
jpayne@7 94
jpayne@7 95 def close(self):
jpayne@7 96 """Cleans up adapter specific items."""
jpayne@7 97 raise NotImplementedError
jpayne@7 98
jpayne@7 99
jpayne@7 100 class HTTPAdapter(BaseAdapter):
jpayne@7 101 """The built-in HTTP Adapter for urllib3.
jpayne@7 102
jpayne@7 103 Provides a general-case interface for Requests sessions to contact HTTP and
jpayne@7 104 HTTPS urls by implementing the Transport Adapter interface. This class will
jpayne@7 105 usually be created by the :class:`Session <Session>` class under the
jpayne@7 106 covers.
jpayne@7 107
jpayne@7 108 :param pool_connections: The number of urllib3 connection pools to cache.
jpayne@7 109 :param pool_maxsize: The maximum number of connections to save in the pool.
jpayne@7 110 :param max_retries: The maximum number of retries each connection
jpayne@7 111 should attempt. Note, this applies only to failed DNS lookups, socket
jpayne@7 112 connections and connection timeouts, never to requests where data has
jpayne@7 113 made it to the server. By default, Requests does not retry failed
jpayne@7 114 connections. If you need granular control over the conditions under
jpayne@7 115 which we retry a request, import urllib3's ``Retry`` class and pass
jpayne@7 116 that instead.
jpayne@7 117 :param pool_block: Whether the connection pool should block for connections.
jpayne@7 118
jpayne@7 119 Usage::
jpayne@7 120
jpayne@7 121 >>> import requests
jpayne@7 122 >>> s = requests.Session()
jpayne@7 123 >>> a = requests.adapters.HTTPAdapter(max_retries=3)
jpayne@7 124 >>> s.mount('http://', a)
jpayne@7 125 """
jpayne@7 126
jpayne@7 127 __attrs__ = [
jpayne@7 128 "max_retries",
jpayne@7 129 "config",
jpayne@7 130 "_pool_connections",
jpayne@7 131 "_pool_maxsize",
jpayne@7 132 "_pool_block",
jpayne@7 133 ]
jpayne@7 134
jpayne@7 135 def __init__(
jpayne@7 136 self,
jpayne@7 137 pool_connections=DEFAULT_POOLSIZE,
jpayne@7 138 pool_maxsize=DEFAULT_POOLSIZE,
jpayne@7 139 max_retries=DEFAULT_RETRIES,
jpayne@7 140 pool_block=DEFAULT_POOLBLOCK,
jpayne@7 141 ):
jpayne@7 142 if max_retries == DEFAULT_RETRIES:
jpayne@7 143 self.max_retries = Retry(0, read=False)
jpayne@7 144 else:
jpayne@7 145 self.max_retries = Retry.from_int(max_retries)
jpayne@7 146 self.config = {}
jpayne@7 147 self.proxy_manager = {}
jpayne@7 148
jpayne@7 149 super().__init__()
jpayne@7 150
jpayne@7 151 self._pool_connections = pool_connections
jpayne@7 152 self._pool_maxsize = pool_maxsize
jpayne@7 153 self._pool_block = pool_block
jpayne@7 154
jpayne@7 155 self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block)
jpayne@7 156
jpayne@7 157 def __getstate__(self):
jpayne@7 158 return {attr: getattr(self, attr, None) for attr in self.__attrs__}
jpayne@7 159
jpayne@7 160 def __setstate__(self, state):
jpayne@7 161 # Can't handle by adding 'proxy_manager' to self.__attrs__ because
jpayne@7 162 # self.poolmanager uses a lambda function, which isn't pickleable.
jpayne@7 163 self.proxy_manager = {}
jpayne@7 164 self.config = {}
jpayne@7 165
jpayne@7 166 for attr, value in state.items():
jpayne@7 167 setattr(self, attr, value)
jpayne@7 168
jpayne@7 169 self.init_poolmanager(
jpayne@7 170 self._pool_connections, self._pool_maxsize, block=self._pool_block
jpayne@7 171 )
jpayne@7 172
jpayne@7 173 def init_poolmanager(
jpayne@7 174 self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs
jpayne@7 175 ):
jpayne@7 176 """Initializes a urllib3 PoolManager.
jpayne@7 177
jpayne@7 178 This method should not be called from user code, and is only
jpayne@7 179 exposed for use when subclassing the
jpayne@7 180 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 181
jpayne@7 182 :param connections: The number of urllib3 connection pools to cache.
jpayne@7 183 :param maxsize: The maximum number of connections to save in the pool.
jpayne@7 184 :param block: Block when no free connections are available.
jpayne@7 185 :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager.
jpayne@7 186 """
jpayne@7 187 # save these values for pickling
jpayne@7 188 self._pool_connections = connections
jpayne@7 189 self._pool_maxsize = maxsize
jpayne@7 190 self._pool_block = block
jpayne@7 191
jpayne@7 192 self.poolmanager = PoolManager(
jpayne@7 193 num_pools=connections,
jpayne@7 194 maxsize=maxsize,
jpayne@7 195 block=block,
jpayne@7 196 **pool_kwargs,
jpayne@7 197 )
jpayne@7 198
jpayne@7 199 def proxy_manager_for(self, proxy, **proxy_kwargs):
jpayne@7 200 """Return urllib3 ProxyManager for the given proxy.
jpayne@7 201
jpayne@7 202 This method should not be called from user code, and is only
jpayne@7 203 exposed for use when subclassing the
jpayne@7 204 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 205
jpayne@7 206 :param proxy: The proxy to return a urllib3 ProxyManager for.
jpayne@7 207 :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager.
jpayne@7 208 :returns: ProxyManager
jpayne@7 209 :rtype: urllib3.ProxyManager
jpayne@7 210 """
jpayne@7 211 if proxy in self.proxy_manager:
jpayne@7 212 manager = self.proxy_manager[proxy]
jpayne@7 213 elif proxy.lower().startswith("socks"):
jpayne@7 214 username, password = get_auth_from_url(proxy)
jpayne@7 215 manager = self.proxy_manager[proxy] = SOCKSProxyManager(
jpayne@7 216 proxy,
jpayne@7 217 username=username,
jpayne@7 218 password=password,
jpayne@7 219 num_pools=self._pool_connections,
jpayne@7 220 maxsize=self._pool_maxsize,
jpayne@7 221 block=self._pool_block,
jpayne@7 222 **proxy_kwargs,
jpayne@7 223 )
jpayne@7 224 else:
jpayne@7 225 proxy_headers = self.proxy_headers(proxy)
jpayne@7 226 manager = self.proxy_manager[proxy] = proxy_from_url(
jpayne@7 227 proxy,
jpayne@7 228 proxy_headers=proxy_headers,
jpayne@7 229 num_pools=self._pool_connections,
jpayne@7 230 maxsize=self._pool_maxsize,
jpayne@7 231 block=self._pool_block,
jpayne@7 232 **proxy_kwargs,
jpayne@7 233 )
jpayne@7 234
jpayne@7 235 return manager
jpayne@7 236
jpayne@7 237 def cert_verify(self, conn, url, verify, cert):
jpayne@7 238 """Verify a SSL certificate. This method should not be called from user
jpayne@7 239 code, and is only exposed for use when subclassing the
jpayne@7 240 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 241
jpayne@7 242 :param conn: The urllib3 connection object associated with the cert.
jpayne@7 243 :param url: The requested URL.
jpayne@7 244 :param verify: Either a boolean, in which case it controls whether we verify
jpayne@7 245 the server's TLS certificate, or a string, in which case it must be a path
jpayne@7 246 to a CA bundle to use
jpayne@7 247 :param cert: The SSL certificate to verify.
jpayne@7 248 """
jpayne@7 249 if url.lower().startswith("https") and verify:
jpayne@7 250
jpayne@7 251 cert_loc = None
jpayne@7 252
jpayne@7 253 # Allow self-specified cert location.
jpayne@7 254 if verify is not True:
jpayne@7 255 cert_loc = verify
jpayne@7 256
jpayne@7 257 if not cert_loc:
jpayne@7 258 cert_loc = extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)
jpayne@7 259
jpayne@7 260 if not cert_loc or not os.path.exists(cert_loc):
jpayne@7 261 raise OSError(
jpayne@7 262 f"Could not find a suitable TLS CA certificate bundle, "
jpayne@7 263 f"invalid path: {cert_loc}"
jpayne@7 264 )
jpayne@7 265
jpayne@7 266 conn.cert_reqs = "CERT_REQUIRED"
jpayne@7 267
jpayne@7 268 if not os.path.isdir(cert_loc):
jpayne@7 269 conn.ca_certs = cert_loc
jpayne@7 270 else:
jpayne@7 271 conn.ca_cert_dir = cert_loc
jpayne@7 272 else:
jpayne@7 273 conn.cert_reqs = "CERT_NONE"
jpayne@7 274 conn.ca_certs = None
jpayne@7 275 conn.ca_cert_dir = None
jpayne@7 276
jpayne@7 277 if cert:
jpayne@7 278 if not isinstance(cert, basestring):
jpayne@7 279 conn.cert_file = cert[0]
jpayne@7 280 conn.key_file = cert[1]
jpayne@7 281 else:
jpayne@7 282 conn.cert_file = cert
jpayne@7 283 conn.key_file = None
jpayne@7 284 if conn.cert_file and not os.path.exists(conn.cert_file):
jpayne@7 285 raise OSError(
jpayne@7 286 f"Could not find the TLS certificate file, "
jpayne@7 287 f"invalid path: {conn.cert_file}"
jpayne@7 288 )
jpayne@7 289 if conn.key_file and not os.path.exists(conn.key_file):
jpayne@7 290 raise OSError(
jpayne@7 291 f"Could not find the TLS key file, invalid path: {conn.key_file}"
jpayne@7 292 )
jpayne@7 293
jpayne@7 294 def build_response(self, req, resp):
jpayne@7 295 """Builds a :class:`Response <requests.Response>` object from a urllib3
jpayne@7 296 response. This should not be called from user code, and is only exposed
jpayne@7 297 for use when subclassing the
jpayne@7 298 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
jpayne@7 299
jpayne@7 300 :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
jpayne@7 301 :param resp: The urllib3 response object.
jpayne@7 302 :rtype: requests.Response
jpayne@7 303 """
jpayne@7 304 response = Response()
jpayne@7 305
jpayne@7 306 # Fallback to None if there's no status_code, for whatever reason.
jpayne@7 307 response.status_code = getattr(resp, "status", None)
jpayne@7 308
jpayne@7 309 # Make headers case-insensitive.
jpayne@7 310 response.headers = CaseInsensitiveDict(getattr(resp, "headers", {}))
jpayne@7 311
jpayne@7 312 # Set encoding.
jpayne@7 313 response.encoding = get_encoding_from_headers(response.headers)
jpayne@7 314 response.raw = resp
jpayne@7 315 response.reason = response.raw.reason
jpayne@7 316
jpayne@7 317 if isinstance(req.url, bytes):
jpayne@7 318 response.url = req.url.decode("utf-8")
jpayne@7 319 else:
jpayne@7 320 response.url = req.url
jpayne@7 321
jpayne@7 322 # Add new cookies from the server.
jpayne@7 323 extract_cookies_to_jar(response.cookies, req, resp)
jpayne@7 324
jpayne@7 325 # Give the Response some context.
jpayne@7 326 response.request = req
jpayne@7 327 response.connection = self
jpayne@7 328
jpayne@7 329 return response
jpayne@7 330
jpayne@7 331 def get_connection(self, url, proxies=None):
jpayne@7 332 """Returns a urllib3 connection for the given URL. This should not be
jpayne@7 333 called from user code, and is only exposed for use when subclassing the
jpayne@7 334 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 335
jpayne@7 336 :param url: The URL to connect to.
jpayne@7 337 :param proxies: (optional) A Requests-style dictionary of proxies used on this request.
jpayne@7 338 :rtype: urllib3.ConnectionPool
jpayne@7 339 """
jpayne@7 340 proxy = select_proxy(url, proxies)
jpayne@7 341
jpayne@7 342 if proxy:
jpayne@7 343 proxy = prepend_scheme_if_needed(proxy, "http")
jpayne@7 344 proxy_url = parse_url(proxy)
jpayne@7 345 if not proxy_url.host:
jpayne@7 346 raise InvalidProxyURL(
jpayne@7 347 "Please check proxy URL. It is malformed "
jpayne@7 348 "and could be missing the host."
jpayne@7 349 )
jpayne@7 350 proxy_manager = self.proxy_manager_for(proxy)
jpayne@7 351 conn = proxy_manager.connection_from_url(url)
jpayne@7 352 else:
jpayne@7 353 # Only scheme should be lower case
jpayne@7 354 parsed = urlparse(url)
jpayne@7 355 url = parsed.geturl()
jpayne@7 356 conn = self.poolmanager.connection_from_url(url)
jpayne@7 357
jpayne@7 358 return conn
jpayne@7 359
jpayne@7 360 def close(self):
jpayne@7 361 """Disposes of any internal state.
jpayne@7 362
jpayne@7 363 Currently, this closes the PoolManager and any active ProxyManager,
jpayne@7 364 which closes any pooled connections.
jpayne@7 365 """
jpayne@7 366 self.poolmanager.clear()
jpayne@7 367 for proxy in self.proxy_manager.values():
jpayne@7 368 proxy.clear()
jpayne@7 369
jpayne@7 370 def request_url(self, request, proxies):
jpayne@7 371 """Obtain the url to use when making the final request.
jpayne@7 372
jpayne@7 373 If the message is being sent through a HTTP proxy, the full URL has to
jpayne@7 374 be used. Otherwise, we should only use the path portion of the URL.
jpayne@7 375
jpayne@7 376 This should not be called from user code, and is only exposed for use
jpayne@7 377 when subclassing the
jpayne@7 378 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 379
jpayne@7 380 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
jpayne@7 381 :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs.
jpayne@7 382 :rtype: str
jpayne@7 383 """
jpayne@7 384 proxy = select_proxy(request.url, proxies)
jpayne@7 385 scheme = urlparse(request.url).scheme
jpayne@7 386
jpayne@7 387 is_proxied_http_request = proxy and scheme != "https"
jpayne@7 388 using_socks_proxy = False
jpayne@7 389 if proxy:
jpayne@7 390 proxy_scheme = urlparse(proxy).scheme.lower()
jpayne@7 391 using_socks_proxy = proxy_scheme.startswith("socks")
jpayne@7 392
jpayne@7 393 url = request.path_url
jpayne@7 394 if is_proxied_http_request and not using_socks_proxy:
jpayne@7 395 url = urldefragauth(request.url)
jpayne@7 396
jpayne@7 397 return url
jpayne@7 398
jpayne@7 399 def add_headers(self, request, **kwargs):
jpayne@7 400 """Add any headers needed by the connection. As of v2.0 this does
jpayne@7 401 nothing by default, but is left for overriding by users that subclass
jpayne@7 402 the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 403
jpayne@7 404 This should not be called from user code, and is only exposed for use
jpayne@7 405 when subclassing the
jpayne@7 406 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 407
jpayne@7 408 :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
jpayne@7 409 :param kwargs: The keyword arguments from the call to send().
jpayne@7 410 """
jpayne@7 411 pass
jpayne@7 412
jpayne@7 413 def proxy_headers(self, proxy):
jpayne@7 414 """Returns a dictionary of the headers to add to any request sent
jpayne@7 415 through a proxy. This works with urllib3 magic to ensure that they are
jpayne@7 416 correctly sent to the proxy, rather than in a tunnelled request if
jpayne@7 417 CONNECT is being used.
jpayne@7 418
jpayne@7 419 This should not be called from user code, and is only exposed for use
jpayne@7 420 when subclassing the
jpayne@7 421 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
jpayne@7 422
jpayne@7 423 :param proxy: The url of the proxy being used for this request.
jpayne@7 424 :rtype: dict
jpayne@7 425 """
jpayne@7 426 headers = {}
jpayne@7 427 username, password = get_auth_from_url(proxy)
jpayne@7 428
jpayne@7 429 if username:
jpayne@7 430 headers["Proxy-Authorization"] = _basic_auth_str(username, password)
jpayne@7 431
jpayne@7 432 return headers
jpayne@7 433
jpayne@7 434 def send(
jpayne@7 435 self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
jpayne@7 436 ):
jpayne@7 437 """Sends PreparedRequest object. Returns Response object.
jpayne@7 438
jpayne@7 439 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
jpayne@7 440 :param stream: (optional) Whether to stream the request content.
jpayne@7 441 :param timeout: (optional) How long to wait for the server to send
jpayne@7 442 data before giving up, as a float, or a :ref:`(connect timeout,
jpayne@7 443 read timeout) <timeouts>` tuple.
jpayne@7 444 :type timeout: float or tuple or urllib3 Timeout object
jpayne@7 445 :param verify: (optional) Either a boolean, in which case it controls whether
jpayne@7 446 we verify the server's TLS certificate, or a string, in which case it
jpayne@7 447 must be a path to a CA bundle to use
jpayne@7 448 :param cert: (optional) Any user-provided SSL certificate to be trusted.
jpayne@7 449 :param proxies: (optional) The proxies dictionary to apply to the request.
jpayne@7 450 :rtype: requests.Response
jpayne@7 451 """
jpayne@7 452
jpayne@7 453 try:
jpayne@7 454 conn = self.get_connection(request.url, proxies)
jpayne@7 455 except LocationValueError as e:
jpayne@7 456 raise InvalidURL(e, request=request)
jpayne@7 457
jpayne@7 458 self.cert_verify(conn, request.url, verify, cert)
jpayne@7 459 url = self.request_url(request, proxies)
jpayne@7 460 self.add_headers(
jpayne@7 461 request,
jpayne@7 462 stream=stream,
jpayne@7 463 timeout=timeout,
jpayne@7 464 verify=verify,
jpayne@7 465 cert=cert,
jpayne@7 466 proxies=proxies,
jpayne@7 467 )
jpayne@7 468
jpayne@7 469 chunked = not (request.body is None or "Content-Length" in request.headers)
jpayne@7 470
jpayne@7 471 if isinstance(timeout, tuple):
jpayne@7 472 try:
jpayne@7 473 connect, read = timeout
jpayne@7 474 timeout = TimeoutSauce(connect=connect, read=read)
jpayne@7 475 except ValueError:
jpayne@7 476 raise ValueError(
jpayne@7 477 f"Invalid timeout {timeout}. Pass a (connect, read) timeout tuple, "
jpayne@7 478 f"or a single float to set both timeouts to the same value."
jpayne@7 479 )
jpayne@7 480 elif isinstance(timeout, TimeoutSauce):
jpayne@7 481 pass
jpayne@7 482 else:
jpayne@7 483 timeout = TimeoutSauce(connect=timeout, read=timeout)
jpayne@7 484
jpayne@7 485 try:
jpayne@7 486 resp = conn.urlopen(
jpayne@7 487 method=request.method,
jpayne@7 488 url=url,
jpayne@7 489 body=request.body,
jpayne@7 490 headers=request.headers,
jpayne@7 491 redirect=False,
jpayne@7 492 assert_same_host=False,
jpayne@7 493 preload_content=False,
jpayne@7 494 decode_content=False,
jpayne@7 495 retries=self.max_retries,
jpayne@7 496 timeout=timeout,
jpayne@7 497 chunked=chunked,
jpayne@7 498 )
jpayne@7 499
jpayne@7 500 except (ProtocolError, OSError) as err:
jpayne@7 501 raise ConnectionError(err, request=request)
jpayne@7 502
jpayne@7 503 except MaxRetryError as e:
jpayne@7 504 if isinstance(e.reason, ConnectTimeoutError):
jpayne@7 505 # TODO: Remove this in 3.0.0: see #2811
jpayne@7 506 if not isinstance(e.reason, NewConnectionError):
jpayne@7 507 raise ConnectTimeout(e, request=request)
jpayne@7 508
jpayne@7 509 if isinstance(e.reason, ResponseError):
jpayne@7 510 raise RetryError(e, request=request)
jpayne@7 511
jpayne@7 512 if isinstance(e.reason, _ProxyError):
jpayne@7 513 raise ProxyError(e, request=request)
jpayne@7 514
jpayne@7 515 if isinstance(e.reason, _SSLError):
jpayne@7 516 # This branch is for urllib3 v1.22 and later.
jpayne@7 517 raise SSLError(e, request=request)
jpayne@7 518
jpayne@7 519 raise ConnectionError(e, request=request)
jpayne@7 520
jpayne@7 521 except ClosedPoolError as e:
jpayne@7 522 raise ConnectionError(e, request=request)
jpayne@7 523
jpayne@7 524 except _ProxyError as e:
jpayne@7 525 raise ProxyError(e)
jpayne@7 526
jpayne@7 527 except (_SSLError, _HTTPError) as e:
jpayne@7 528 if isinstance(e, _SSLError):
jpayne@7 529 # This branch is for urllib3 versions earlier than v1.22
jpayne@7 530 raise SSLError(e, request=request)
jpayne@7 531 elif isinstance(e, ReadTimeoutError):
jpayne@7 532 raise ReadTimeout(e, request=request)
jpayne@7 533 elif isinstance(e, _InvalidHeader):
jpayne@7 534 raise InvalidHeader(e, request=request)
jpayne@7 535 else:
jpayne@7 536 raise
jpayne@7 537
jpayne@7 538 return self.build_response(request, resp)