CYh]6ddlmZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z ddl mZddlmZdd lmZmZmZdd lmZmZmZmZdd lmZdd lmZdd lmZddl m!Z!ddl"m#Z#ddl$m%Z%m&Z&ejNr ddl(Z(ddl)m*Z*gdZ+ejXe-Z.dZ/dZ0ejbdZ2GddejfZ4 ddZ5ejle5e4ejle5e4dZ7eedZ8GddeZ9Gdde9Z:d dZ;y)!) annotationsN) TracebackType)urljoin)HTTPHeaderDictRecentlyUsedContainer)RequestMethods) ProxyConfig)HTTPConnectionPoolHTTPSConnectionPoolport_by_scheme)LocationValueError MaxRetryErrorProxySchemeUnknownURLSchemeUnknown)BaseHTTPResponse)_TYPE_SOCKET_OPTIONS)connection_requires_http_tunnel)Retry)Timeout)Url parse_url)Literal) PoolManager ProxyManagerproxy_from_url) key_file cert_file cert_reqsca_certs ssl_versionssl_minimum_versionssl_maximum_version ca_cert_dir ssl_context key_passwordserver_hostnamei@_SelfTc*eZdZUdZded<ded<ded<ded<d ed <d ed <d ed<ded<ded<ded<ded<ded<ded<ded<ded<ded<ded<ded<ded<d ed!<ded"<d#ed$<d%ed&<ded'<d(ed)<ded*<ded+<ded,<y-).PoolKeyz All known keyword arguments that could be provided to the pool manager, its pools, or the underlying connections. All custom key schemes should include the fields in this key at a minimum. str key_schemekey_host int | Nonekey_portzTimeout | float | int | None key_timeoutzRetry | bool | int | None key_retriesz bool | None key_blockztuple[str, int] | Nonekey_source_address str | None key_key_filekey_key_password key_cert_file key_cert_reqs key_ca_certszint | str | Nonekey_ssl_versionzssl.TLSVersion | Nonekey_ssl_minimum_versionkey_ssl_maximum_versionkey_ca_cert_dirssl.SSLContext | Nonekey_ssl_context key_maxsizez!frozenset[tuple[str, str]] | None key_headers Url | None key__proxykey__proxy_headersProxyConfig | Nonekey__proxy_configz_TYPE_SOCKET_OPTIONS | Nonekey_socket_optionskey__socks_optionszbool | str | Nonekey_assert_hostnamekey_assert_fingerprintkey_server_hostname key_blocksizeN)__name__ __module__ __qualname____doc____annotations__5/usr/lib/python3/dist-packages/urllib3/poolmanager.pyr*r*9sOM--**..  %%2222**2299))3399**&&##rSr*c|j}|dj|d<|dj|d<dD],}||vs||t||j||<.|j d}|t ||d<t |jD]}|j||d|z<|jD] }||vsd||<|j d t|d<|di|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_rLrR) copylower frozensetitemsgettuplelistkeyspop_fields_DEFAULT_BLOCKSIZE) key_classrequest_contextcontextkey socket_optsfields rT_default_key_normalizerrn_s'.""$G)//1GHfo++-GFO?; '>gcl6$WS\%7%7%9:GCL; ++./K$)+$6 !GLLN#1 ' C 0 1"""  !GEN" {{?#+#5  w rShttphttpsc.eZdZUdZdZded<dZded< d dfd ZddZ dd Z d dd Z dd Z d dd Z dd Z ddZ d ddZ ddZd dZ d! d"dZxZS)#raV 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: .. code-block:: python import urllib3 http = urllib3.PoolManager(num_pools=2) resp1 = http.request("GET", "https://google.com/") resp2 = http.request("GET", "https://google.com/mail") resp3 = http.request("GET", "https://yahoo.com/") print(len(http.pools)) # 2 NrBproxyrE proxy_configc >t||d|vrL|d}t|ts7|du}tj|d}||_|j }||d<||_|t||_ t|_ tj |_ y)NretriesFredirect) super__init__ isinstancerfrom_intraise_on_redirectr]connection_pool_kwrpoolspool_classes_by_schemekey_fn_by_scheme)self num_poolsrXr~rvr} __class__s rTrzzPoolManager.__init__s ! * *(3Ggu-%,5$8!..5A,=)%7%<%<%>"07"9-"4 *95 '=# 0 5 5 7rSc|SNrRrs rT __enter__zPoolManager.__enter__s rSc$|jy)NF)clear)rexc_typeexc_valexc_tbs rT__exit__zPoolManager.__exit__s rSc|j|}||jj}|jd t|d<dD]}|j |d|dk(rt D]}|j |d|||fi|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 blocksize)rVrWportrp)rr~r]rargre SSL_KEYWORDS)rrVrWrripool_clsrkkws rT _new_poolzPoolManager._new_pools .2-H-H-P  ""55::$*$4f!!%%oh&?&E&E&GLD"&"&++O<"     ) *002#4488@#"6* *'8,,X,WWrSc|jj5|jj|}|r |cdddS|d}|d}|d}|j||||}||j|<ddd|S#1swYSxYw)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. NrVrWrr)rlockrar)rrripoolrVrWrs rTrz$PoolManager.connection_from_pool_keyZsZZ__ (::>>(+D ( (%X.F"6*D"6*D>>&$o>VD#'DJJx  (  ( sA=3A==Bc~t|}|j|j|j|j|S)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. )rrVr)rrrWrrV)rurlrus rTconnection_from_urlzPoolManager.connection_from_urlts: cN(( FFk)  rSc|jj}|r$|jD]\}}| ||= |||<|S#t$rY!wxYw)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. )r~r]r`KeyError)roverridebase_pool_kwargsrkvalues rTrzPoolManager._merge_pool_kwargssq 22779 &nn. 2 U=,S1-2$S) 2  $sA AAct|jyt|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. F)rsrrtrV)r parsed_urls rT!_proxy_requires_url_absolute_formz-PoolManager._proxy_requires_url_absolute_forms: :: 2 JJ)):+<+<   rSc t|}|jtjdtd|j |j |j|j}d|d<d|d<d |vr|j|d <|j|r|j||fi|}n|j||jfi|}|xr|j}|s|St||}|jd k(r&d }d|d <t|d j!|d <|j#d |j$} t'| t(st)j*| |} | j,rb|j/|sQ|d j1} |d D]1} | j3| j,vs | j5| d3| |d < | j7||||} | |d <||d<t>jAd|||j=|j||fi|S#t8$r"| j:r|j=|cYSwxYw)aN Same as :meth:`urllib3.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. Na URLs without a scheme (ie 'https://') are deprecated and will raise an error in a future version of urllib3. To avoid this DeprecationWarning ensure all URLs start with 'https://' or 'http://'. Read more in this issue: https://github.com/urllib3/urllib3/issues/2920)category stacklevel)rrVFassert_same_hostrxrXi/GETbodyrvrw)response_poolzRedirecting %s -> %s)!rrVrrrrrWrrXrurlopen request_uriget_redirect_locationrstatusr_prepare_for_method_changerarvr{rr|remove_headers_on_redirect is_same_hostr]r^re incrementrr} drain_connloginfo) rmethodrrxrrconnrredirect_locationrv new_headersheaders rTrzPoolManager.urlopensV cN 88  MMA,  ((affQXX(N!& : B  LLByM  1 1! 4#t||FC626H#t||FAMM@R@H$I)G)G)I O$C):; ??c !FBvJ*2i=9TTVByM&&H$4$45'5)nnWx@G  - -d6G6G 7 Y-,,.KY- 2<<>W%G%GGOOFD1 2(ByM ''hd'SG 9 !: '.?@t||F$5<<< ((##%O  sH,,(II) N)rintrXtyping.Mapping[str, str] | Noner~ typing.AnyreturnNone)rr(rr()rztype[BaseException] | NonerzBaseException | NonerzTracebackType | NonerzLiteral[False]r) rVr+rWr+rrridict[str, typing.Any] | Nonerr )rrNrpN rWr4rr.rVr4rrrr )ridict[str, typing.Any]rr )rr*rirrr )rr+rrrr )rrrr)rrrboolT rr+rr+rxrrrrr)rMrNrOrPrsrQrtrzrrrrrrrrrrr __classcell__rs@rTrrsBE:'+L$+378818) 8  8B,&%   9= $7$7$7 $7 6 $7  $7L #48 === = 2 =  =:X4X X22G 6EI  %A  $ 4  ,  7;P=P= #P=/3P=BLP= P=rSrceZdZdZ d dfd Z d d fd Z d d dZ d d fd ZxZS)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. :param proxy_assert_hostname: The hostname of the certificate to verify against. :param proxy_assert_fingerprint: The fingerprint of the certificate to verify against. Example: .. code-block:: python import urllib3 proxy = urllib3.ProxyManager("https://localhost:3128/") resp1 = proxy.request("GET", "https://google.com/") resp2 = proxy.request("GET", "https://httpbin.org/") print(len(proxy.pools)) # 1 resp3 = proxy.request("GET", "https://httpbin.org/") resp4 = proxy.request("GET", "https://twitter.com/") print(len(proxy.pools)) # 3 c  (t|tr)|jd|jd|j} n|} t | } | jdvrt | j| js2tj| jd} | j| } | |_ |xsi|_ ||_ t|||||_|j| d<|j| d<|j| d<t |@||fi| y) Nz://:ror)r_proxyrY _proxy_config)r{r rVrWrrrr ra_replacers proxy_headersproxy_ssl_contextr rtryrz)r proxy_urlrrXrruse_forwarding_for_httpsproxy_assert_hostnameproxy_assert_fingerprintr~ str_proxy_urlrsrrs rTrzzProxyManager.__init__3s  i!3 4(//0INN3C1Y^^DTUM%M-( <<0 0$U\\2 2zz!%%ellB7DNNN-E *0b!2'  $ ! $  (,zz8$/3/A/A+,.2.?.??+ GB/ABrSc|dk(rt|||||St||jj|jj|jj |S)Nrq)r)ryrrsrWrrV)rrWrrVrrs rTrz!ProxyManager.connection_from_host\si W 7/dF 0 w+ JJOOTZZ__djj.?.?[,  rSclddi}t|j}|r||d<|r|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)rrrXheaders_rs rT_set_proxy_headerszProxyManager._set_proxy_headersls>e$3&& %HV   OOG $rSc t|}t|j|j|js1|j d|j }|j|||d<t|$||fd|i|S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rXrx) rrrsrtrVrarXrryr)rrrrxrrrXrs rTrzProxyManager.urlopen}so cN.tzz4;L;LahhWffY 5G 33CAByMwvsDXDDDrS)rNNNFNN)rr+rrrXrrrrr>rrrzNone | str | Literal[False]rr4r~rrrrrr)rr+rXrrztyping.Mapping[str, str]rr) rMrNrOrPrzrrrrrs@rTrrs54r379=37).=A/3'C'C'C1 'C 7 'C 1 'C#''C ;'C#-'C)'C 'CX #48      2    "DH!@ !$7; E E # E/3 EBL E  E ErSrc tdd|i|S)NrrR)r)rrs rTrrs  ,# , ,,rS)rhz type[PoolKey]rirrr*)rr+rrrr)< __future__r functoolsloggingtypingrtypesr urllib.parser _collectionsrr_request_methodsr connectionr connectionpoolr r r exceptionsrrrrrrutil.connectionr util.proxyr util.retryr util.timeoutrutil.urlrr TYPE_CHECKINGssltyping_extensionsr__all__ getLoggerrMrrrgTypeVarr( NamedTupler*rnpartialrrrrrrRrSrTr s""  ?,#SS '17!$ ) ;g!    !#f#L4 4 /D4  4 x I  5w ? Y  6 @ #5?RSW=.W=t ME;ME`-rS