a Ba_`S@sdZddlZddlZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1m2Z2mZmZm3Z3m4Z4m5Z5m6Z6ddl7m8Z8zddl9m:Z:Wne;yddZ:Yn0dZdZ?Gd d!d!e@ZAGd"d#d#eAZBdS)$z requests.adapters ~~~~~~~~~~~~~~~~~ This module contains the transport adapters that Requests uses to define and maintain connections. N) PoolManagerproxy_from_url) HTTPResponse) parse_url)Timeout)Retry)ClosedPoolError)ConnectTimeoutError) HTTPError) MaxRetryError)NewConnectionError) ProxyError) ProtocolError)ReadTimeoutError)SSLError) ResponseError)LocationValueError)Response)urlparse basestring)DEFAULT_CA_BUNDLE_PATHextract_zipped_pathsget_encoding_from_headersprepend_scheme_if_neededget_auth_from_url urldefragauth select_proxy)CaseInsensitiveDict)extract_cookies_to_jar) ConnectionErrorConnectTimeout ReadTimeoutrr RetryError InvalidSchemaInvalidProxyURL InvalidURL)_basic_auth_str)SOCKSProxyManagercOs tddS)Nz'Missing dependencies for SOCKS support.)r$)argskwargsr+3/usr/lib/python3/dist-packages/requests/adapters.pyr(.sr(F cs2eZdZdZfddZd ddZd d ZZS) BaseAdapterzThe Base Transport Adaptercstt|dSN)superr.__init__self __class__r+r,r1:szBaseAdapter.__init__FNTcCstdS)aCSends PreparedRequest object. Returns Response object. :param request: The :class:`PreparedRequest ` being sent. :param stream: (optional) Whether to stream the request content. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) ` tuple. :type timeout: float or tuple :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: (optional) Any user-provided SSL certificate to be trusted. :param proxies: (optional) The proxies dictionary to apply to the request. NNotImplementedError)r3requeststreamtimeoutverifycertproxiesr+r+r,send=szBaseAdapter.sendcCstdS)z!Cleans up adapter specific items.Nr6r2r+r+r,closeOszBaseAdapter.close)FNTNN)__name__ __module__ __qualname____doc__r1r>r? __classcell__r+r+r4r,r.7s   r.cseZdZdZgdZeeeeffdd ZddZ ddZ efd d Z d d Z d dZ ddZd ddZddZddZddZddZd!ddZZS)" HTTPAdapteraThe built-in HTTP Adapter for urllib3. Provides a general-case interface for Requests sessions to contact HTTP and HTTPS urls by implementing the Transport Adapter interface. This class will usually be created by the :class:`Session ` class under the covers. :param pool_connections: The number of urllib3 connection pools to cache. :param pool_maxsize: The maximum number of connections to save in the pool. :param max_retries: The maximum number of retries each connection should attempt. Note, this applies only to failed DNS lookups, socket connections and connection timeouts, never to requests where data has made it to the server. By default, Requests does not retry failed connections. If you need granular control over the conditions under which we retry a request, import urllib3's ``Retry`` class and pass that instead. :param pool_block: Whether the connection pool should block for connections. Usage:: >>> import requests >>> s = requests.Session() >>> a = requests.adapters.HTTPAdapter(max_retries=3) >>> s.mount('http://', a) ) max_retriesconfig_pool_connections _pool_maxsize _pool_blockcsd|tkrtddd|_n t||_i|_i|_tt|||_ ||_ ||_ |j |||ddS)NrF)readblock) DEFAULT_RETRIESrrFZfrom_intrG proxy_managerr0rEr1rHrIrJinit_poolmanager)r3Zpool_connectionsZ pool_maxsizerFZ pool_blockr4r+r,r1qs zHTTPAdapter.__init__csfddjDS)Ncsi|]}|t|dqSr/)getattr).0attrr2r+r, z,HTTPAdapter.__getstate__..) __attrs__r2r+r2r, __getstate__szHTTPAdapter.__getstate__cCsDi|_i|_|D]\}}t|||q|j|j|j|jddS)NrL)rOrGitemssetattrrPrHrIrJ)r3staterSvaluer+r+r, __setstate__s zHTTPAdapter.__setstate__cKs0||_||_||_tf|||dd||_dS)aInitializes a urllib3 PoolManager. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param connections: The number of urllib3 connection pools to cache. :param maxsize: The maximum number of connections to save in the pool. :param block: Block when no free connections are available. :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager. T) num_poolsmaxsizerMstrictN)rHrIrJr poolmanager)r3Z connectionsr^rMZ pool_kwargsr+r+r,rPs zHTTPAdapter.init_poolmanagercKs||jvr|j|}n||dr^t|\}}t|f|||j|j|jd|}|j|<n4||}t |f||j|j|jd|}|j|<|S)aReturn urllib3 ProxyManager for the given proxy. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param proxy: The proxy to return a urllib3 ProxyManager for. :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager. :returns: ProxyManager :rtype: urllib3.ProxyManager socks)usernamepasswordr]r^rM) proxy_headersr]r^rM) rOlower startswithrr(rHrIrJrdr)r3proxyZ proxy_kwargsZmanagerrbrcrdr+r+r,proxy_manager_fors6    zHTTPAdapter.proxy_manager_forcCs|drn|rnd}|dur"|}|s.tt}|r>tj|sLtd|d|_ tj |sf||_ q||_ nd|_ d|_ d|_ |rt |ts|d|_|d|_n ||_d|_|jrtj|jstd |j|jrtj|jstd |jdS) aAVerify a SSL certificate. This method should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param conn: The urllib3 connection object associated with the cert. :param url: The requested URL. :param verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: The SSL certificate to verify. httpsNTzECould not find a suitable TLS CA certificate bundle, invalid path: {}Z CERT_REQUIREDZ CERT_NONErrz9Could not find the TLS certificate file, invalid path: {}z1Could not find the TLS key file, invalid path: {})rerfrrospathexistsIOErrorformatZ cert_reqsisdirZca_certsZ ca_cert_dir isinstancerZ cert_fileZkey_file)r3connurlr;r<Zcert_locr+r+r, cert_verifys>     zHTTPAdapter.cert_verifycCst}t|dd|_tt|di|_t|j|_||_|jj|_t |j t r^|j d|_ n|j |_ t |j||||_||_|S)aBuilds a :class:`Response ` object from a urllib3 response. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter ` :param req: The :class:`PreparedRequest ` used to generate the response. :param resp: The urllib3 response object. :rtype: requests.Response ZstatusNheadersutf-8)rrQZ status_coderrtrencodingrawreasonrprrbytesdecodercookiesr8 connection)r3ZreqrespZresponser+r+r,build_responses    zHTTPAdapter.build_responseNcCsdt||}|rDt|d}t|}|js.td||}||}nt|}|}|j |}|S)aReturns a urllib3 connection for the given URL. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param url: The URL to connect to. :param proxies: (optional) A Requests-style dictionary of proxies used on this request. :rtype: urllib3.ConnectionPool ZhttpzFPlease check proxy URL. It is malformed and could be missing the host.) rrrZhostr%rhZconnection_from_urlrZgeturlr`)r3rrr=rgZ proxy_urlrOrqZparsedr+r+r,get_connection$s     zHTTPAdapter.get_connectioncCs&|j|jD] }|qdS)zDisposes of any internal state. Currently, this closes the PoolManager and any active ProxyManager, which closes any pooled connections. N)r`clearrOvalues)r3rgr+r+r,r??s zHTTPAdapter.closec Cs`t|j|}t|jj}|o"|dk}d}|rDt|j}|d}|j}|r\|s\t|j}|S)a?Obtain the url to use when making the final request. If the message is being sent through a HTTP proxy, the full URL has to be used. Otherwise, we should only use the path portion of the URL. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param request: The :class:`PreparedRequest ` being sent. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. :rtype: str riFra)rrrrschemererfZpath_urlr) r3r8r=rgrZis_proxied_http_requestZusing_socks_proxyZ proxy_schemerrr+r+r, request_urlIs     zHTTPAdapter.request_urlcKsdS)a"Add any headers needed by the connection. As of v2.0 this does nothing by default, but is left for overriding by users that subclass the :class:`HTTPAdapter `. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param request: The :class:`PreparedRequest ` to add headers to. :param kwargs: The keyword arguments from the call to send(). Nr+)r3r8r*r+r+r, add_headersfs zHTTPAdapter.add_headerscCs&i}t|\}}|r"t|||d<|S)aReturns a dictionary of the headers to add to any request sent through a proxy. This works with urllib3 magic to ensure that they are correctly sent to the proxy, rather than in a tunnelled request if CONNECT is being used. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param proxy: The url of the proxy being used for this request. :rtype: dict zProxy-Authorization)rr')r3rgrtrbrcr+r+r,rdts  zHTTPAdapter.proxy_headersFTc Csz||j|}Wn0tyB}zt||dWYd}~n d}~00|||j|||||} |j||||||d|jdupd|jv } t |t rz|\} } t | | d}Wn6t y}zd |} t | WYd}~n d}~00nt |t rn t ||d}zD| s6|j|j| |j|jdddd|j|d }nt|d rH|j}|jtd }z|j|j| d d |jD]\}}|||qr||jD]D}|tt|d dd|d|||dq|dz|jd d}Wnty|}Yn0tj |||ddd}Wn|!Yn0Wnt"t#j$fy} zt%| |dWYd} ~ njd} ~ 0t&y(}zt |j't(rt |j't)st*||dt |j't+rt,||dt |j't-rt.||dt |j't/rt0||dt%||dWYd}~nd}~0t1yX}zt%||dWYd}~nd}~0t-y}zt.|WYd}~nhd}~0t/t2fy}zBt |t/rt0||dnt |t3rt4||dnWYd}~n d}~00|5||S)aSends PreparedRequest object. Returns Response object. :param request: The :class:`PreparedRequest ` being sent. :param stream: (optional) Whether to stream the request content. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) ` tuple. :type timeout: float or tuple or urllib3 Timeout object :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: (optional) Any user-provided SSL certificate to be trusted. :param proxies: (optional) The proxies dictionary to apply to the request. :rtype: requests.Response )r8N)r9r:r;r<r=zContent-Length)connectrKzrInvalid timeout {}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valueF) methodrrbodyrtZredirectZassert_same_hostpreload_contentdecode_contentZretriesr: proxy_pool)r:T)Zskip_accept_encodingrus s0 ) buffering)Zpoolr|rr)6rrrrr&rsrrrrtrptuple TimeoutSauce ValueErrorrnZurlopenrrFhasattrrZ _get_connDEFAULT_POOL_TIMEOUTZ putrequestrXZ putheaderZ endheadersr>hexlenencodeZ getresponse TypeErrorrZ from_httplibr?rsocketerrorr r rxr r r!rr# _ProxyErrorr _SSLErrorrr _HTTPErrorrr"r~)r3r8r9r:r;r<r=rqerrZchunkedrrKerrr}Zlow_connheaderr[irr+r+r,r>s"              "        zHTTPAdapter.send)N)FNTNN)r@rArBrCrVDEFAULT_POOLSIZErNDEFAULT_POOLBLOCKr1rWr\rPrhrsr~rr?rrrdr>rDr+r+r4r,rETs$ %4%  rE)CrCos.pathrjrZurllib3.poolmanagerrrZurllib3.responserZ urllib3.utilrrrZurllib3.util.retryrZurllib3.exceptionsrr r rr r r rrrrrrrZmodelsrcompatrrZutilsrrrrrrrZ structuresrr{r exceptionsr r!r"r#r$r%r&Zauthr'Zurllib3.contrib.socksr( ImportErrorrrrNrobjectr.rEr+r+r+r,sF                $  ,