t_E@sddlmZddlZddlZddlZddlZddlmZddlm Z m Z ddlm Z ddl m Z mZmZmZddlmZdd lmZdd lmZdd lmZdd lmZd ddgZejeZd9Zd:Z ej!d1e Z"d2d3Z#d4ej$e#e"d5ej$e#e"iZ%d4e d5e iZ&Gd6d d eZ'Gd7dde'Z(d8dZ)dS);)absolute_importN)RecentlyUsedContainer)HTTPConnectionPoolHTTPSConnectionPool)port_by_scheme)LocationValueError MaxRetryErrorProxySchemeUnknown InvalidProxyConfigurationWarning)six)urljoin)RequestMethods) parse_url)Retry PoolManager ProxyManagerproxy_from_urlkey_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|j}|dj|d<|dj|d>> 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 cKsPtj||||_t|ddd|_t|_tj|_dS)N dispose_funccSs |jS)N)close)prMrMrNsz&PoolManager.__init__..)r__init__connection_pool_kwrpoolspool_classes_by_schemekey_fn_by_schemer=)self num_poolsr8rXrMrMrNrWs   zPoolManager.__init__cCs|S)NrM)r\rMrMrN __enter__szPoolManager.__enter__cCs|jdS)NF)clear)r\exc_typeexc_valexc_tbrMrMrN__exit__s zPoolManager.__exit__cCs|j|}|dkr(|jj}xdD]}|j|dq/W|dkrvxtD]}|j|dq\W||||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. Nr6r7portrP)r6r7rd)rZrXr=rE SSL_KEYWORDS)r\r6r7rdrHpool_clsrJkwrMrMrN _new_pools     zPoolManager._new_poolcCs|jjdS)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)rYr_)r\rMrMrNr_szPoolManager.clearrPcCst|std|j|}|p*d|d<|sStj|djd}||d<||d<|j|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.rPr6Prdr7)r_merge_pool_kwargsrrAr>connection_from_context)r\r7rdr6 pool_kwargsrHrMrMrNconnection_from_hosts    z PoolManager.connection_from_hostcCs<|dj}|j|}||}|j|d|S)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_keyrMrMrNrks  z#PoolManager.connection_from_contextc Csz|jjg|jj|}|r)|S|d}|d}|d}|j|||d|}||j| %s)rrmr7rdr6r8r=proxyurlopen request_uriget_redirect_locationr statusrA isinstancerfrom_intremove_headers_on_redirect is_same_hostrCr iterkeysr>rE incrementr raise_on_redirect drain_connloginfo) r\methodrsr{rgrtconnr~redirect_locationr}r8headerrMrMrNr;sH $     "       zPoolManager.urlopen)__name__ __module__ __qualname____doc__rrWr^rcrhr_rmrkrnrurjrrMrMrMrNrs      cs|eZdZdZdddfddZdddfddZdd d Zd d Zd fddZS)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 rRNc st|tr+d|j|j|jf}t|}|jsgtj|jd}|jd|}|jdkrt |j||_ |pi|_ |j |d<|j |d|j||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\rsr8headers_rrMrMrN_set_proxy_headerss   zProxyManager._set_proxy_headerscCs8|dkr4|jjdkr4tjdtdddS)NrQaYour 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 sxt|}|j|j|jdkrV|jd|j}|j|||dsp    " 6n