Path: blob/master/venv/Lib/site-packages/urllib3/poolmanager.py
811 views
from __future__ import absolute_import1import collections2import functools3import logging4import warnings56from ._collections import RecentlyUsedContainer7from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool8from .connectionpool import port_by_scheme9from .exceptions import (10LocationValueError,11MaxRetryError,12ProxySchemeUnknown,13InvalidProxyConfigurationWarning,14)15from .packages import six16from .packages.six.moves.urllib.parse import urljoin17from .request import RequestMethods18from .util.url import parse_url19from .util.retry import Retry202122__all__ = ["PoolManager", "ProxyManager", "proxy_from_url"]232425log = logging.getLogger(__name__)2627SSL_KEYWORDS = (28"key_file",29"cert_file",30"cert_reqs",31"ca_certs",32"ssl_version",33"ca_cert_dir",34"ssl_context",35"key_password",36)3738# All known keyword arguments that could be provided to the pool manager, its39# pools, or the underlying connections. This is used to construct a pool key.40_key_fields = (41"key_scheme", # str42"key_host", # str43"key_port", # int44"key_timeout", # int or float or Timeout45"key_retries", # int or Retry46"key_strict", # bool47"key_block", # bool48"key_source_address", # str49"key_key_file", # str50"key_key_password", # str51"key_cert_file", # str52"key_cert_reqs", # str53"key_ca_certs", # str54"key_ssl_version", # str55"key_ca_cert_dir", # str56"key_ssl_context", # instance of ssl.SSLContext or urllib3.util.ssl_.SSLContext57"key_maxsize", # int58"key_headers", # dict59"key__proxy", # parsed proxy url60"key__proxy_headers", # dict61"key_socket_options", # list of (level (int), optname (int), value (int or str)) tuples62"key__socks_options", # dict63"key_assert_hostname", # bool or string64"key_assert_fingerprint", # str65"key_server_hostname", # str66)6768#: The namedtuple class used to construct keys for the connection pool.69#: All custom key schemes should include the fields in this key at a minimum.70PoolKey = collections.namedtuple("PoolKey", _key_fields)717273def _default_key_normalizer(key_class, request_context):74"""75Create a pool key out of a request context dictionary.7677According to RFC 3986, both the scheme and host are case-insensitive.78Therefore, this function normalizes both before constructing the pool79key for an HTTPS request. If you wish to change this behaviour, provide80alternate callables to ``key_fn_by_scheme``.8182:param key_class:83The class to use when constructing the key. This should be a namedtuple84with the ``scheme`` and ``host`` keys at a minimum.85:type key_class: namedtuple86:param request_context:87A dictionary-like object that contain the context for a request.88:type request_context: dict8990:return: A namedtuple that can be used as a connection pool key.91:rtype: PoolKey92"""93# Since we mutate the dictionary, make a copy first94context = request_context.copy()95context["scheme"] = context["scheme"].lower()96context["host"] = context["host"].lower()9798# These are both dictionaries and need to be transformed into frozensets99for key in ("headers", "_proxy_headers", "_socks_options"):100if key in context and context[key] is not None:101context[key] = frozenset(context[key].items())102103# The socket_options key may be a list and needs to be transformed into a104# tuple.105socket_opts = context.get("socket_options")106if socket_opts is not None:107context["socket_options"] = tuple(socket_opts)108109# Map the kwargs to the names in the namedtuple - this is necessary since110# namedtuples can't have fields starting with '_'.111for key in list(context.keys()):112context["key_" + key] = context.pop(key)113114# Default to ``None`` for keys missing from the context115for field in key_class._fields:116if field not in context:117context[field] = None118119return key_class(**context)120121122#: A dictionary that maps a scheme to a callable that creates a pool key.123#: This can be used to alter the way pool keys are constructed, if desired.124#: Each PoolManager makes a copy of this dictionary so they can be configured125#: globally here, or individually on the instance.126key_fn_by_scheme = {127"http": functools.partial(_default_key_normalizer, PoolKey),128"https": functools.partial(_default_key_normalizer, PoolKey),129}130131pool_classes_by_scheme = {"http": HTTPConnectionPool, "https": HTTPSConnectionPool}132133134class PoolManager(RequestMethods):135"""136Allows for arbitrary requests while transparently keeping track of137necessary connection pools for you.138139:param num_pools:140Number of connection pools to cache before discarding the least141recently used pool.142143:param headers:144Headers to include with all requests, unless other headers are given145explicitly.146147:param \\**connection_pool_kw:148Additional parameters are used to create fresh149:class:`urllib3.connectionpool.ConnectionPool` instances.150151Example::152153>>> manager = PoolManager(num_pools=2)154>>> r = manager.request('GET', 'http://google.com/')155>>> r = manager.request('GET', 'http://google.com/mail')156>>> r = manager.request('GET', 'http://yahoo.com/')157>>> len(manager.pools)1582159160"""161162proxy = None163164def __init__(self, num_pools=10, headers=None, **connection_pool_kw):165RequestMethods.__init__(self, headers)166self.connection_pool_kw = connection_pool_kw167self.pools = RecentlyUsedContainer(num_pools, dispose_func=lambda p: p.close())168169# Locally set the pool classes and keys so other PoolManagers can170# override them.171self.pool_classes_by_scheme = pool_classes_by_scheme172self.key_fn_by_scheme = key_fn_by_scheme.copy()173174def __enter__(self):175return self176177def __exit__(self, exc_type, exc_val, exc_tb):178self.clear()179# Return False to re-raise any potential exceptions180return False181182def _new_pool(self, scheme, host, port, request_context=None):183"""184Create a new :class:`ConnectionPool` based on host, port, scheme, and185any additional pool keyword arguments.186187If ``request_context`` is provided, it is provided as keyword arguments188to the pool class used. This method is used to actually create the189connection pools handed out by :meth:`connection_from_url` and190companion methods. It is intended to be overridden for customization.191"""192pool_cls = self.pool_classes_by_scheme[scheme]193if request_context is None:194request_context = self.connection_pool_kw.copy()195196# Although the context has everything necessary to create the pool,197# this function has historically only used the scheme, host, and port198# in the positional args. When an API change is acceptable these can199# be removed.200for key in ("scheme", "host", "port"):201request_context.pop(key, None)202203if scheme == "http":204for kw in SSL_KEYWORDS:205request_context.pop(kw, None)206207return pool_cls(host, port, **request_context)208209def clear(self):210"""211Empty our store of pools and direct them all to close.212213This will not affect in-flight connections, but they will not be214re-used after completion.215"""216self.pools.clear()217218def connection_from_host(self, host, port=None, scheme="http", pool_kwargs=None):219"""220Get a :class:`ConnectionPool` based on the host, port, and scheme.221222If ``port`` isn't given, it will be derived from the ``scheme`` using223``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is224provided, it is merged with the instance's ``connection_pool_kw``225variable and used to create the new connection pool, if one is226needed.227"""228229if not host:230raise LocationValueError("No host specified.")231232request_context = self._merge_pool_kwargs(pool_kwargs)233request_context["scheme"] = scheme or "http"234if not port:235port = port_by_scheme.get(request_context["scheme"].lower(), 80)236request_context["port"] = port237request_context["host"] = host238239return self.connection_from_context(request_context)240241def connection_from_context(self, request_context):242"""243Get a :class:`ConnectionPool` based on the request context.244245``request_context`` must at least contain the ``scheme`` key and its246value must be a key in ``key_fn_by_scheme`` instance variable.247"""248scheme = request_context["scheme"].lower()249pool_key_constructor = self.key_fn_by_scheme[scheme]250pool_key = pool_key_constructor(request_context)251252return self.connection_from_pool_key(pool_key, request_context=request_context)253254def connection_from_pool_key(self, pool_key, request_context=None):255"""256Get a :class:`ConnectionPool` based on the provided pool key.257258``pool_key`` should be a namedtuple that only contains immutable259objects. At a minimum it must have the ``scheme``, ``host``, and260``port`` fields.261"""262with self.pools.lock:263# If the scheme, host, or port doesn't match existing open264# connections, open a new ConnectionPool.265pool = self.pools.get(pool_key)266if pool:267return pool268269# Make a fresh ConnectionPool of the desired type270scheme = request_context["scheme"]271host = request_context["host"]272port = request_context["port"]273pool = self._new_pool(scheme, host, port, request_context=request_context)274self.pools[pool_key] = pool275276return pool277278def connection_from_url(self, url, pool_kwargs=None):279"""280Similar to :func:`urllib3.connectionpool.connection_from_url`.281282If ``pool_kwargs`` is not provided and a new pool needs to be283constructed, ``self.connection_pool_kw`` is used to initialize284the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``285is provided, it is used instead. Note that if a new pool does not286need to be created for the request, the provided ``pool_kwargs`` are287not used.288"""289u = parse_url(url)290return self.connection_from_host(291u.host, port=u.port, scheme=u.scheme, pool_kwargs=pool_kwargs292)293294def _merge_pool_kwargs(self, override):295"""296Merge a dictionary of override values for self.connection_pool_kw.297298This does not modify self.connection_pool_kw and returns a new dict.299Any keys in the override dictionary with a value of ``None`` are300removed from the merged dictionary.301"""302base_pool_kwargs = self.connection_pool_kw.copy()303if override:304for key, value in override.items():305if value is None:306try:307del base_pool_kwargs[key]308except KeyError:309pass310else:311base_pool_kwargs[key] = value312return base_pool_kwargs313314def urlopen(self, method, url, redirect=True, **kw):315"""316Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`317with custom cross-host redirect logic and only sends the request-uri318portion of the ``url``.319320The given ``url`` parameter must be absolute, such that an appropriate321:class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.322"""323u = parse_url(url)324conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)325326kw["assert_same_host"] = False327kw["redirect"] = False328329if "headers" not in kw:330kw["headers"] = self.headers.copy()331332if self.proxy is not None and u.scheme == "http":333response = conn.urlopen(method, url, **kw)334else:335response = conn.urlopen(method, u.request_uri, **kw)336337redirect_location = redirect and response.get_redirect_location()338if not redirect_location:339return response340341# Support relative URLs for redirecting.342redirect_location = urljoin(url, redirect_location)343344# RFC 7231, Section 6.4.4345if response.status == 303:346method = "GET"347348retries = kw.get("retries")349if not isinstance(retries, Retry):350retries = Retry.from_int(retries, redirect=redirect)351352# Strip headers marked as unsafe to forward to the redirected location.353# Check remove_headers_on_redirect to avoid a potential network call within354# conn.is_same_host() which may use socket.gethostbyname() in the future.355if retries.remove_headers_on_redirect and not conn.is_same_host(356redirect_location357):358headers = list(six.iterkeys(kw["headers"]))359for header in headers:360if header.lower() in retries.remove_headers_on_redirect:361kw["headers"].pop(header, None)362363try:364retries = retries.increment(method, url, response=response, _pool=conn)365except MaxRetryError:366if retries.raise_on_redirect:367response.drain_conn()368raise369return response370371kw["retries"] = retries372kw["redirect"] = redirect373374log.info("Redirecting %s -> %s", url, redirect_location)375376response.drain_conn()377return self.urlopen(method, redirect_location, **kw)378379380class ProxyManager(PoolManager):381"""382Behaves just like :class:`PoolManager`, but sends all requests through383the defined proxy, using the CONNECT method for HTTPS URLs.384385:param proxy_url:386The URL of the proxy to be used.387388:param proxy_headers:389A dictionary containing headers that will be sent to the proxy. In case390of HTTP they are being sent with each request, while in the391HTTPS/CONNECT case they are sent only once. Could be used for proxy392authentication.393394Example:395>>> proxy = urllib3.ProxyManager('http://localhost:3128/')396>>> r1 = proxy.request('GET', 'http://google.com/')397>>> r2 = proxy.request('GET', 'http://httpbin.org/')398>>> len(proxy.pools)3991400>>> r3 = proxy.request('GET', 'https://httpbin.org/')401>>> r4 = proxy.request('GET', 'https://twitter.com/')402>>> len(proxy.pools)4033404405"""406407def __init__(408self,409proxy_url,410num_pools=10,411headers=None,412proxy_headers=None,413**connection_pool_kw414):415416if isinstance(proxy_url, HTTPConnectionPool):417proxy_url = "%s://%s:%i" % (418proxy_url.scheme,419proxy_url.host,420proxy_url.port,421)422proxy = parse_url(proxy_url)423if not proxy.port:424port = port_by_scheme.get(proxy.scheme, 80)425proxy = proxy._replace(port=port)426427if proxy.scheme not in ("http", "https"):428raise ProxySchemeUnknown(proxy.scheme)429430self.proxy = proxy431self.proxy_headers = proxy_headers or {}432433connection_pool_kw["_proxy"] = self.proxy434connection_pool_kw["_proxy_headers"] = self.proxy_headers435436super(ProxyManager, self).__init__(num_pools, headers, **connection_pool_kw)437438def connection_from_host(self, host, port=None, scheme="http", pool_kwargs=None):439if scheme == "https":440return super(ProxyManager, self).connection_from_host(441host, port, scheme, pool_kwargs=pool_kwargs442)443444return super(ProxyManager, self).connection_from_host(445self.proxy.host, self.proxy.port, self.proxy.scheme, pool_kwargs=pool_kwargs446)447448def _set_proxy_headers(self, url, headers=None):449"""450Sets headers needed by proxies: specifically, the Accept and Host451headers. Only sets headers not provided by the user.452"""453headers_ = {"Accept": "*/*"}454455netloc = parse_url(url).netloc456if netloc:457headers_["Host"] = netloc458459if headers:460headers_.update(headers)461return headers_462463def _validate_proxy_scheme_url_selection(self, url_scheme):464if url_scheme == "https" and self.proxy.scheme == "https":465warnings.warn(466"Your proxy configuration specified an HTTPS scheme for the proxy. "467"Are you sure you want to use HTTPS to contact the proxy? "468"This most likely indicates an error in your configuration. "469"Read this issue for more info: "470"https://github.com/urllib3/urllib3/issues/1850",471InvalidProxyConfigurationWarning,472stacklevel=3,473)474475def urlopen(self, method, url, redirect=True, **kw):476"Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."477u = parse_url(url)478self._validate_proxy_scheme_url_selection(u.scheme)479480if u.scheme == "http":481# For proxied HTTPS requests, httplib sets the necessary headers482# on the CONNECT to the proxy. For HTTP, we'll definitely483# need to set 'Host' at the very least.484headers = kw.get("headers", self.headers)485kw["headers"] = self._set_proxy_headers(url, headers)486487return super(ProxyManager, self).urlopen(method, url, redirect=redirect, **kw)488489490def proxy_from_url(url, **kw):491return ProxyManager(proxy_url=url, **kw)492493494