B `3M@s8ddlmZddlZddlZddlZddlmZddlmZm Z m Z ddl m Z m Z mZmZmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZd ddgZeeZ dZ!dZ"e#de"Z$dZ%e#de%Z&ddZ'e(e'e$e(e'e$dZ)ee dZ*Gdd d eZ+Gddde+Z,ddZ-dS))absolute_importN)RecentlyUsedContainer)HTTPConnectionPoolHTTPSConnectionPoolport_by_scheme)LocationValueError MaxRetryErrorProxySchemeUnknownProxySchemeUnsupportedURLSchemeUnknown)six)urljoin)RequestMethods)connection_requires_http_tunnel)Retry) parse_url PoolManager ProxyManagerproxy_from_url)Zkey_fileZ cert_fileZ cert_reqsZca_certsZ ssl_versionZ ca_cert_dir ssl_contextZ key_password)Z key_schemeZkey_hostZkey_portZ key_timeoutZ key_retriesZ key_strictZ key_blockZkey_source_addressZ key_key_fileZkey_key_passwordZ key_cert_fileZ key_cert_reqsZ key_ca_certsZkey_ssl_versionZkey_ca_cert_dirZkey_ssl_contextZ key_maxsizeZ key_headersZ key__proxyZkey__proxy_headersZkey__proxy_configZkey_socket_optionsZkey__socks_optionsZkey_assert_hostnameZkey_assert_fingerprintZkey_server_hostnamePoolKey)ruse_forwarding_for_https ProxyConfigcCs|}|d|d<|d|d<x4dD],}||kr.||dk r.t||||<q.W|d}|dk r|t||d<x&t|D]}|||d|<qWx|j D]}||krd||<qW|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_headersZ_socks_optionsNZsocket_optionsZkey_) copylower frozensetitemsgettuplelistkeyspop_fields)Z key_classrequest_contextcontextkeyZ socket_optsfieldr,D/opt/alt/python37/lib/python3.7/site-packages/urllib3/poolmanager.py_default_key_normalizerNs     r.)httphttpsc@seZdZdZdZdZd ddZddZdd Zd!d d Z d d Z d"ddZ ddZ d#ddZ d$ddZddZddZddZd%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|S)N)close)pr,r,r-z&PoolManager.__init__..)Z dispose_func)r__init__connection_pool_kwrpoolspool_classes_by_schemekey_fn_by_schemer)self num_poolsrr7r,r,r-r6s  zPoolManager.__init__cCs|S)Nr,)r;r,r,r- __enter__szPoolManager.__enter__cCs |dS)NF)clear)r;exc_typeexc_valexc_tbr,r,r-__exit__szPoolManager.__exit__cCsf|j|}|dkr|j}xdD]}||dq"W|dkrXxtD]}||dqDW|||f|S)a Create a new :class:`urllib3.connectionpool.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)rrportr/)r9r7rr& SSL_KEYWORDS)r;rrrCr(Zpool_clsr*kwr,r,r- _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)r8r>)r;r,r,r-r>szPoolManager.clearr/cCsT|s td||}|pd|d<|s:t|dd}||d<||d<||S)a Get a :class:`urllib3.connectionpool.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.r/rPrCr)r_merge_pool_kwargsrr"rconnection_from_context)r;rrCr pool_kwargsr(r,r,r-connection_from_hosts   z PoolManager.connection_from_hostcCs:|d}|j|}|s$t|||}|j||dS)z Get a :class:`urllib3.connectionpool.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. r)r()rr:r"r connection_from_pool_key)r;r(rZpool_key_constructorpool_keyr,r,r-rIs   z#PoolManager.connection_from_contextc Cs`|jjN|j|}|r|S|d}|d}|d}|j||||d}||j|<WdQRX|S)a Get a :class:`urllib3.connectionpool.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. rrrC)r(N)r8lockr"rF)r;rMr(poolrrrCr,r,r-rLs  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. )rCrrJ)rrKrrCr)r;urlrJur,r,r-connection_from_urls zPoolManager.connection_from_urlc CsZ|j}|rVxF|D]:\}}|dkrJy ||=WqRtk rFYqRXq|||<qW|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)r7rr!KeyError)r;overrideZbase_pool_kwargsr*valuer,r,r-rH.s   zPoolManager._merge_pool_kwargscCs"|jdkrdSt|j|j|j S)z Indicates if the proxy requires the complete destination URL in the request. Normally this is only needed when not using an HTTP CONNECT tunnel. NF)proxyr proxy_configr)r; parsed_urlr,r,r-!_proxy_requires_url_absolute_formBs z-PoolManager._proxy_requires_url_absolute_formcCs@|jdks|dkrdS|jjdkr&dStjr<|jjs %s)rr[rrKrrCrrrYurlopenZ request_uriZget_redirect_locationrstatusr" isinstancerZfrom_intZremove_headers_on_redirectZ is_same_hostr$r iterkeysrr&Z incrementr Zraise_on_redirectZ drain_connloginfo) r;methodrPr\rErQZconnr^Zredirect_locationr]rheaderr,r,r-r_`sJ          zPoolManager.urlopen)r1N)N)Nr/N)N)N)T)__name__ __module__ __qualname____doc__rVrWr6r=rBrFr>rKrIrLrRrHrYr[r_r,r,r,r-rs       csHeZdZdZdfdd Zdfdd Zdd d Zdfd d ZZS)ra> 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. :param proxy_ssl_context: The proxy SSL context is used to establish the TLS connection to the proxy when using HTTPS proxies. :param use_forwarding_for_https: (Defaults to False) If set to True will forward requests to the HTTPS proxy to be made on behalf of the client instead of creating a TLS tunnel via the CONNECT method. **Enabling this flag means that request and response headers and content will be visible from the HTTPS proxy** whereas tunneling keeps request and response headers and content private. IP address, target hostname, SNI, and port are always visible to an HTTPS proxy even when this flag is disabled. 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 r1NFc  st|trd|j|j|jf}t|}|jdkr:t|j|jsZt|jd} |j | d}||_ |pfi|_ ||_ t |||_|j |d<|j |d<|j|d<tt|j||f|dS)Nz %s://%s:%i)r/r0rG)rC_proxyrZ _proxy_config)rarrrrCrr rr"_replacerV proxy_headersproxy_ssl_contextrrWsuperrr6) r; proxy_urlr<rrmrnrr7rVrC) __class__r,r-r6s&          zProxyManager.__init__r/csD|dkr tt|j||||dStt|j|jj|jj|jj|dS)Nr0)rJ)rorrKrVrrCr)r;rrCrrJ)rqr,r-rKs   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. ZAcceptz*/*ZHost)rnetlocupdate)r;rPrZheaders_rrr,r,r-_set_proxy_headerss  zProxyManager._set_proxy_headersTc sVt|}t|j|j|js8|d|j}||||d<tt |j ||fd|i|S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rr\) rrrVrWrr"rrtrorr_)r;rerPr\rErQr)rqr,r-r_ s zProxyManager.urlopen)r1NNNF)Nr/N)N)T) rgrhrirjr6rKrtr_ __classcell__r,r,)rqr-rs& cKstfd|i|S)Nrp)r)rPrEr,r,r-rs). __future__r collections functoolslogging _collectionsrZconnectionpoolrrr exceptionsrr r r r packagesr Zpackages.six.moves.urllib.parserZrequestrZ util.proxyrZ util.retryrZutil.urlr__all__ getLoggerrgrcrDZ _key_fields namedtuplerZ_proxy_config_fieldsrr.partialr:r9rrrr,r,r,r-s8             6  s