U Őd_E@s(ddlmZddlZddlZddlZddlZddlmZddlm Z m Z ddlm Z ddl m Z mZmZmZddlmZdd lmZdd lmZdd lmZdd lmZd ddgZeeZdZdZ e!de Z"ddZ#e$e#e"e$e#e"dZ%e e dZ&Gdd d eZ'Gddde'Z(ddZ)dS))absolute_importN)RecentlyUsedContainer)HTTPConnectionPoolHTTPSConnectionPool)port_by_scheme)LocationValueError MaxRetryErrorProxySchemeUnknown InvalidProxyConfigurationWarning)six)urljoin)RequestMethods) parse_url)Retry PoolManager ProxyManagerproxy_from_url)key_file cert_file cert_reqsca_certs ssl_version ca_cert_dir ssl_context key_password) key_schemekey_hostkey_port key_timeout key_retries key_strict key_blockkey_source_address key_key_filekey_key_password key_cert_file key_cert_reqs key_ca_certskey_ssl_versionkey_ca_cert_dirkey_ssl_context key_maxsize key_headers key__proxykey__proxy_headerskey_socket_optionskey__socks_optionskey_assert_hostnamekey_assert_fingerprintkey_server_hostnamePoolKeycCs|}|d|d<|d|d<dD],}||kr,||dk r,t||||<q,|d}|dk rxt||d<t|D]}|||d|<q|j D]}||krd||<q|f|S)a Create a pool key out of a request context dictionary. According to RFC 3986, both the scheme and host are case-insensitive. Therefore, this function normalizes both before constructing the pool key for an HTTPS request. If you wish to change this behaviour, provide alternate callables to ``key_fn_by_scheme``. :param key_class: The class to use when constructing the key. This should be a namedtuple with the ``scheme`` and ``host`` keys at a minimum. :type key_class: namedtuple :param request_context: A dictionary-like object that contain the context for a request. :type request_context: dict :return: A namedtuple that can be used as a connection pool key. :rtype: PoolKey schemehost)headers_proxy_headers_socks_optionsNsocket_optionskey_) copylower frozensetitemsgettuplelistkeyspop_fields) key_classrequest_contextcontextkey socket_optsfieldrMZC:\Users\aemmanux\AppData\Local\Temp\pip-target-bnng1y30\lib\python\urllib3/poolmanager.py_default_key_normalizerJs    rOhttphttpsc@sxeZdZdZdZdddZddZdd Zdd d Zd d Z dddZ ddZ dddZ d ddZ ddZd!ddZdS)"ra$ Allows for arbitrary requests while transparently keeping track of necessary connection pools for you. :param num_pools: Number of connection pools to cache before discarding the least recently used pool. :param headers: Headers to include with all requests, unless other headers are given explicitly. :param \**connection_pool_kw: Additional parameters are used to create fresh :class:`urllib3.connectionpool.ConnectionPool` instances. Example:: >>> manager = PoolManager(num_pools=2) >>> r = manager.request('GET', 'http://google.com/') >>> r = manager.request('GET', 'http://google.com/mail') >>> r = manager.request('GET', 'http://yahoo.com/') >>> len(manager.pools) 2 N cKs8t||||_t|ddd|_t|_t|_dS)NcSs|SN)close)prMrMrNz&PoolManager.__init__..) dispose_func)r__init__connection_pool_kwrpoolspool_classes_by_schemekey_fn_by_schemer=)self num_poolsr8r[rMrMrNrZs  zPoolManager.__init__cCs|SrTrMr_rMrMrN __enter__szPoolManager.__enter__cCs |dS)NF)clear)r_exc_typeexc_valexc_tbrMrMrN__exit__szPoolManager.__exit__cCs^|j|}|dkr|j}dD]}||dq |dkrPtD]}||dq>|||f|S)a Create a new :class:`ConnectionPool` based on host, port, scheme, and any additional pool keyword arguments. If ``request_context`` is provided, it is provided as keyword arguments to the pool class used. This method is used to actually create the connection pools handed out by :meth:`connection_from_url` and companion methods. It is intended to be overridden for customization. N)r6r7portrQ)r]r[r=rE SSL_KEYWORDS)r_r6r7rhrHpool_clsrJkwrMrMrN _new_pools  zPoolManager._new_poolcCs|jdS)z Empty our store of pools and direct them all to close. This will not affect in-flight connections, but they will not be re-used after completion. N)r\rcrarMrMrNrcszPoolManager.clearrQcCsT|s td||}|pd|d<|s:t|dd}||d<||d<||S)a Get a :class:`ConnectionPool` based on the host, port, and scheme. If ``port`` isn't given, it will be derived from the ``scheme`` using ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is provided, it is merged with the instance's ``connection_pool_kw`` variable and used to create the new connection pool, if one is needed. zNo host specified.rQr6Prhr7)r_merge_pool_kwargsrrAr>connection_from_context)r_r7rhr6 pool_kwargsrHrMrMrNconnection_from_hosts   z PoolManager.connection_from_hostcCs,|d}|j|}||}|j||dS)z Get a :class:`ConnectionPool` based on the request context. ``request_context`` must at least contain the ``scheme`` key and its value must be a key in ``key_fn_by_scheme`` instance variable. r6rH)r>r^connection_from_pool_key)r_rHr6pool_key_constructorpool_keyrMrMrNros  z#PoolManager.connection_from_contextc Csl|jjZ|j|}|r*|W5QRS|d}|d}|d}|j||||d}||j|<W5QRX|S)z Get a :class:`ConnectionPool` based on the provided pool key. ``pool_key`` should be a namedtuple that only contains immutable objects. At a minimum it must have the ``scheme``, ``host``, and ``port`` fields. r6r7rhrr)r\lockrArl)r_rurHpoolr6r7rhrMrMrNrss  z$PoolManager.connection_from_pool_keycCs t|}|j|j|j|j|dS)a Similar to :func:`urllib3.connectionpool.connection_from_url`. If ``pool_kwargs`` is not provided and a new pool needs to be constructed, ``self.connection_pool_kw`` is used to initialize the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs`` is provided, it is used instead. Note that if a new pool does not need to be created for the request, the provided ``pool_kwargs`` are not used. )rhr6rp)rrqr7rhr6)r_urlrpurMrMrNconnection_from_urls zPoolManager.connection_from_urlc CsV|j}|rR|D]:\}}|dkrHz ||=WqPtk rDYqPXq|||<q|S)a Merge a dictionary of override values for self.connection_pool_kw. This does not modify self.connection_pool_kw and returns a new dict. Any keys in the override dictionary with a value of ``None`` are removed from the merged dictionary. N)r[r=r@KeyError)r_overridebase_pool_kwargsrJvaluerMrMrNrn's   zPoolManager._merge_pool_kwargsTc Kst|}|j|j|j|jd}d|d<d|d<d|krD|j|d<|jdk rj|jdkrj|j||f|}n|j||j f|}|o| }|s|St ||}|j dkrd }| d } t| tstj| |d } | jr||stt|d} | D]"} | | jkr|d| dqz| j||||d } Wn,tk rZ| jrR||YSX| |d <||d<td ||||j||f|S)a] Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen` with custom cross-host redirect logic and only sends the request-uri portion of the ``url``. The given ``url`` parameter must be absolute, such that an appropriate :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it. )rhr6Fassert_same_hostredirectr8NrQi/GETretries)r)response_poolzRedirecting %s -> %s)rrqr7rhr6r8r=proxyurlopen request_uriget_redirect_locationr statusrA isinstancerfrom_intremove_headers_on_redirect is_same_hostrCr iterkeysr>rE incrementr raise_on_redirect drain_connloginfo) r_methodrxrrkryconnrredirect_locationrr8headerrMrMrNr;sJ        zPoolManager.urlopen)rSN)N)NrQN)N)N)T)__name__ __module__ __qualname____doc__rrZrbrgrlrcrqrorsrzrnrrMrMrMrNrs     csPeZdZdZdfdd Zdfdd Zdd d Zd d Zdfdd ZZ S)rax Behaves just like :class:`PoolManager`, but sends all requests through the defined proxy, using the CONNECT method for HTTPS URLs. :param proxy_url: The URL of the proxy to be used. :param proxy_headers: A dictionary containing headers that will be sent to the proxy. In case of HTTP they are being sent with each request, while in the HTTPS/CONNECT case they are sent only once. Could be used for proxy authentication. Example: >>> proxy = urllib3.ProxyManager('http://localhost:3128/') >>> r1 = proxy.request('GET', 'http://google.com/') >>> r2 = proxy.request('GET', 'http://httpbin.org/') >>> len(proxy.pools) 1 >>> r3 = proxy.request('GET', 'https://httpbin.org/') >>> r4 = proxy.request('GET', 'https://twitter.com/') >>> len(proxy.pools) 3 rSNc st|trd|j|j|jf}t|}|jsFt|jd}|j|d}|jdkrZt |j||_ |pfi|_ |j |d<|j |d<t t |j||f|dS)Nz %s://%s:%irm)rhrP_proxyr9)rrr6r7rhrrrA_replacer r proxy_headerssuperrrZ)r_ proxy_urlr`r8rr[rrh __class__rMrNrZs"       zProxyManager.__init__rQcsD|dkr tt|j||||dStt|j|jj|jj|jj|dS)NrR)rp)rrrqrr7rhr6)r_r7rhr6rprrMrNrqs  z!ProxyManager.connection_from_hostcCs0ddi}t|j}|r||d<|r,|||S)z Sets headers needed by proxies: specifically, the Accept and Host headers. Only sets headers not provided by the user. Acceptz*/*Host)rnetlocupdate)r_rxr8headers_rrMrMrN_set_proxy_headerss  zProxyManager._set_proxy_headerscCs(|dkr$|jjdkr$tjdtdddS)NrRaYour proxy configuration specified an HTTPS scheme for the proxy. Are you sure you want to use HTTPS to contact the proxy? This most likely indicates an error in your configuration. Read this issue for more info: https://github.com/urllib3/urllib3/issues/1850) stacklevel)rr6warningswarnr )r_ url_schemerMrMrN$_validate_proxy_scheme_url_selections z1ProxyManager._validate_proxy_scheme_url_selectionTc sZt|}||j|jdkr<|d|j}||||d<tt|j||fd|i|S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rQr8r) rrr6rAr8rrrr)r_rrxrrkryr8rrMrNrs   zProxyManager.urlopen)rSNN)NrQN)N)T) rrrrrZrqrrr __classcell__rMrMrrNr}s  cKstfd|i|S)Nr)r)rxrkrMrMrNrs)* __future__r collections functoolsloggingr _collectionsrconnectionpoolrrr exceptionsrr r r packagesr Zpackages.six.moves.urllib.parser requestrZutil.urlrZ util.retryr__all__ getLoggerrrri _key_fields namedtupler5rOpartialr^r]rrrrMrMrMrNs6            6   wn