YЅ_c@sdZddlZddlmZddlmZmZddlmZddl m Z ddl m Z dd lmZmZdd lmZmZmZmZmZdd lmZdd lmZdd lmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z#ddlm$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*m"Z"mZm+Z+ddl,m-Z-e.Z/dZ0dZ1de2fdYZ3de3fdYZ4dS(s requests.adapters ~~~~~~~~~~~~~~~~~ This module contains the transport adapters that Requests uses to define and maintain connections. iNi(tResponse(t PoolManagertproxy_from_url(t HTTPResponse(tTimeout(tRetry(turlparset basestring(tDEFAULT_CA_BUNDLE_PATHtget_encoding_from_headerstprepend_scheme_if_neededtget_auth_from_urlt urldefragauth(tCaseInsensitiveDict(tConnectTimeoutError(t HTTPError(t MaxRetryError(t ProxyError(t ProtocolError(tReadTimeoutError(tSSLError(t ResponseError(textract_cookies_to_jar(tConnectionErrortConnectTimeoutt ReadTimeoutRRt RetryError(t_basic_auth_stri it BaseAdaptercBs)eZdZdZdZdZRS(sThe Base Transport AdaptercCstt|jdS(N(tsuperRt__init__(tself((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR+scCs tdS(N(tNotImplementedError(R((s5/usr/lib/python2.7/site-packages/requests/adapters.pytsend.scCs tdS(N(R (R((s5/usr/lib/python2.7/site-packages/requests/adapters.pytclose1s(t__name__t __module__t__doc__RR!R"(((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR(s  t HTTPAdaptercBseZdZdddddgZeeeedZdZdZ ed Z d Z d Z d Z dd ZdZdZdZdZededddZRS(sThe 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 int 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) t max_retriestconfigt_pool_connectionst _pool_maxsizet _pool_blockcCs|tkr$tddt|_ntj||_i|_i|_tt|j ||_ ||_ ||_ |j ||d|dS(Nitreadtblock(tDEFAULT_RETRIESRtFalseR'tfrom_intR(t proxy_managerRR&RR)R*R+tinit_poolmanager(Rtpool_connectionst pool_maxsizeR't pool_block((s5/usr/lib/python2.7/site-packages/requests/adapters.pyRRs      cstfdjDS(Nc3s'|]}|t|dfVqdS(N(tgetattrtNone(t.0tattr(R(s5/usr/lib/python2.7/site-packages/requests/adapters.pys es(tdictt __attrs__(R((Rs5/usr/lib/python2.7/site-packages/requests/adapters.pyt __getstate__dscCsbi|_i|_x*|jD]\}}t|||qW|j|j|jd|jdS(NR-(R1R(titemstsetattrR2R)R*R+(RtstateR9tvalue((s5/usr/lib/python2.7/site-packages/requests/adapters.pyt __setstate__hs   c KsF||_||_||_td|d|d|dt||_dS(sInitializes 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_poolstmaxsizeR-tstrictN(R)R*R+RtTruet poolmanager(Rt connectionsRCR-t pool_kwargs((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR2ts   c Ksc||jkrX|j|}t|d|d|jd|jd|j||j|`. :param proxy: The proxy to return a urllib3 ProxyManager for. :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager. :returns: ProxyManager t proxy_headersRBRCR-(R1RIRR)R*R+(Rtproxyt proxy_kwargsRI((s5/usr/lib/python2.7/site-packages/requests/adapters.pytproxy_manager_fors   cCs|jjdro|rod}|tk r6|}n|sEt}n|sZtdnd|_||_nd|_d|_|rt|t s|d|_ |d|_ q||_ ndS(sVerify 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: Whether we should actually verify the certificate. :param cert: The SSL certificate to verify. thttpss4Could not find a suitable SSL CA certificate bundle.t CERT_REQUIREDt CERT_NONEiiN( tlowert startswithR7RERt Exceptiont cert_reqstca_certst isinstanceRt cert_filetkey_file(Rtconnturltverifytcerttcert_loc((s5/usr/lib/python2.7/site-packages/requests/adapters.pyt cert_verifys"         cCst}t|dd|_tt|di|_t|j|_||_|jj |_ t |j t r|j j d|_ n |j |_ t|j||||_||_|S(sBuilds 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. tstatustheaderssutf-8N(RR6R7t status_codeR R_R tencodingtrawtreasonRURYtbytestdecodeRtcookiestrequestt connection(Rtreqtresptresponse((s5/usr/lib/python2.7/site-packages/requests/adapters.pytbuild_responses     cCs|p i}|jt|jj}|r`t|d}|j|}|j|}n*t|}|j}|jj|}|S(seReturns 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. thttp( tgetRRPtschemeR RLtconnection_from_urltgeturlRF(RRYtproxiesRJR1RXtparsed((s5/usr/lib/python2.7/site-packages/requests/adapters.pytget_connections   cCs|jjdS(sDisposes of any internal state. Currently, this just closes the PoolManager, which closes pooled connections. N(RFtclear(R((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR"scCs^|p i}t|jj}|j|}|rQ|dkrQt|j}n |j}|S(sObtain 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 to proxy URLs. RM(RRYRoRnR tpath_url(RRgRrRoRJRY((s5/usr/lib/python2.7/site-packages/requests/adapters.pyt request_urls  cKsdS(s"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(). N((RRgtkwargs((s5/usr/lib/python2.7/site-packages/requests/adapters.pyt add_headerss cCs>i}t|\}}|r:|r:t|||d`. :param proxies: The url of the proxy being used for this request. :param kwargs: Optional additional keyword arguments. sProxy-Authorization(R R(RRJR_tusernametpassword((s5/usr/lib/python2.7/site-packages/requests/adapters.pyRI-s  cCs|j|j|}|j||j|||j||}|j||jdkphd|jk } t|t ry%|\} } t d| d| }Wqt k r} dj |} t | qXnt d|d|}y| sI|j d|jd|d|jd|jd td td td td |jd| }nIt|drd|j}n|jd|}y|j|j|dtx-|jjD]\}}|j||qW|jx^|jD]S}|jtt|djd|jd|j||jdqW|jd|j}tj|d|d|d td t}Wn|j nX|j!|Wn!t"t#j$fk r} t%| d|nt&k r5} t| j't(rt)| d|nt| j't*r t+| d|nt%| d|nt,k rS} t-| ndt.t/fk r} t| t.rt0| d|qt| t1rt2| d|qnX|j3||S(sSends 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 (`connect timeout, read timeout `_) tuple. :type timeout: float or tuple :param verify: (optional) Whether to verify SSL certificates. :param cert: (optional) Any user-provided SSL certificate to be trusted. :param proxies: (optional) The proxies dictionary to apply to the request. sContent-LengthtconnectR,ssInvalid timeout {0}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valuetmethodRYtbodyR_tredirecttassert_same_hosttpreload_contenttdecode_contenttretriesttimeoutt proxy_pooltskip_accept_encodingisutf-8s s0 tpoolRhRgN(4RtRYR]RwRyR~R7R_RUttuplet TimeoutSaucet ValueErrortformatturlopenR}R/R'thasattrRt _get_connt putrequestRER=t putheadert endheadersR!thextlentencodet getresponseRt from_httplibR"t _put_connRtsocketterrorRRRcRRRRt _ProxyErrorRt _SSLErrort _HTTPErrorRRRRl(RRgtstreamRRZR[RrRXRYtchunkedR|R,teterrRjtlow_conntheaderR@titr((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR!Cs            &      N(R#R$R%R;tDEFAULT_POOLSIZER.tDEFAULT_POOLBLOCKRR<RAR2RLR]RlR7RtR"RwRyRIR/RER!(((s5/usr/lib/python2.7/site-packages/requests/adapters.pyR&5s$       % $     (5R%RtmodelsRturllib3.poolmanagerRRturllib3.responseRt urllib3.utilRRturllib3.util.retryRtcompatRRtutilsRR R R R t structuresR turllib3.exceptionsRRRRRRRRRRRRfRt exceptionsRRRRtauthRR/RRR.tobjectRR&(((s5/usr/lib/python2.7/site-packages/requests/adapters.pyt s2 (.