ZЅ_c@sdZddlZddlmZddlmZddlmZddlmZm Z m Z m Z ddl m Z mZmZmZdd lmZmZmZdd lmZmZdd lmZmZmZmZdd lmZmZm Z m!Z!dd l"m#Z#ddl$m%Z%ddl&m'Z'ddlm(Z(m)Z)m*Z*m+Z+m,Z,ddl-m.Z.ddlm/Z/dZ0e dZ1e dZ2de3fdYZ4de4fdYZ5dZ6dS(s requests.session ~~~~~~~~~~~~~~~~ This module provides a Session object to manage and persist settings across requests (cookies, auth, proxies). iN(tMapping(tdatetimei(t_basic_auth_str(t cookielibt OrderedDictturljointurlparse(tcookiejar_from_dicttextract_cookies_to_jartRequestsCookieJart merge_cookies(tRequesttPreparedRequesttDEFAULT_REDIRECT_LIMIT(t default_hookst dispatch_hook(tto_key_val_listtdefault_headerstto_native_stringt DEFAULT_PORTS(tTooManyRedirectst InvalidSchematChunkedEncodingErrortContentDecodingError(tRecentlyUsedContainer(tCaseInsensitiveDict(t HTTPAdapter(t requote_uritget_environ_proxiestget_netrc_authtshould_bypass_proxiestget_auth_from_url(tcodes(tREDIRECT_STATIicCs|dkr|S|dkr |St|to;t|tsB|S|t|}|jt|x0|jD]"\}}|dkrt||=qtqtWtd|jD}|S(s Determines appropriate setting for a given request, taking into account the explicit setting on that request, and the setting in the session. If a setting is a dictionary, they will be merged together using `dict_class` css-|]#\}}|dk r||fVqdS(N(tNone(t.0tktv((s5/usr/lib/python2.7/site-packages/requests/sessions.pys FsN(R"t isinstanceRRtupdatetitemstdict(trequest_settingtsession_settingt dict_classtmerged_settingR$R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt merge_setting*s   cCsZ|dks!|jdgkr%|S|dksF|jdgkrJ|St|||S(s Properly merges both requests and session hooks. This is necessary because when request_hooks == {'response': []}, the merge breaks Session hooks entirely. tresponseN(R"tgetR.(t request_hookst session_hooksR,((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt merge_hooksKs !!tSessionRedirectMixincBs;eZdZededddZdZdZRS(cCst|}t|}|j|jkr.tS|jdkrn|jdkrn|jdkrn|jdkrntS|j|jk}|j|jk}tj|jddf}| r|j|kr|j|krtS|p|S(sFDecide whether Authorization header should be removed when redirectingthttpiPthttpsiN(iPN(iN( RthostnametTruetschemetportR"tFalseRR0(tselftold_urltnew_urlt old_parsedt new_parsedt changed_porttchanged_schemet default_port((s5/usr/lib/python2.7/site-packages/requests/sessions.pytshould_strip_auth]s  ccs;d}g} x(|jr6|j} |dkrU| j|t| } | |_ny |jWn-tttfk r|j j dt nX||j krt d|j n|j|jd} |j} | jdr t|j}d|j| f} nt| }|j} |jsEt|jt| } n t| } t| | _|jr|j| jkr| j|j|j>> import requests >>> s = requests.Session() >>> s.get('http://httpbin.org/get') 200 R\RmtauthROthookstparamsRMRNtprefetchtadaptersRKR}RZcCst|_d|_i|_t|_i|_t|_ t |_ d|_ t |_t |_ti|_t|_|jdt|jdttt|_dS(Nshttps://shttp://(RR\R"RRORRRR;RKR8RMRNR RZR}RRmRRtmountRRtREDIRECT_CACHE_SIZERc(R<((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt__init__3s           cCs|S(N((R<((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt __enter__iscGs|jdS(N(R[(R<targs((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt__exit__lscCs*|jp i}t|tjs0t|}nttt|j|}|j}|jr| r|j rt |j }nt }|j d|j jd|j d|jd|jd|jdt|j|jdtdt|j|jd t||jd |d t|j|j |S( sbConstructs a :class:`PreparedRequest ` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request ` instance and those of the :class:`Session`. :param request: :class:`Request` instance to prepare with this session's settings. R]R_tfilestdatatjsonR\R,RRRmR(RmR&Rt CookieJarRR R RR}RR_R tprepareR]tupperRRRR.R\RRR3R(R<R|Rmtmerged_cookiesRtp((s5/usr/lib/python2.7/site-packages/requests/sessions.pytprepare_requestos*        cCst|}td|jd|d|d|d|p9id|d|pKid|d |d | }|j|}| p{i} |j|j| | ||}i| d 6| d 6}|j||j||}|S( sCConstructs a :class:`Request `, prepares it and sends it. Returns :class:`Response ` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :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 allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. R]R_R\RRRRRRmRRLRP(RR RRtmerge_environment_settingsR_R'Rq(R<R]R_RRR\RmRRRLRPRORRKRMRNRRstpreptsettingst send_kwargsRr((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR|s,2        cKs#|jdt|jd||S(sSends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPRH(RR8R|(R<R_tkwargs((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR0scKs#|jdt|jd||S(sSends a OPTIONS request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPtOPTIONS(RR8R|(R<R_R((s5/usr/lib/python2.7/site-packages/requests/sessions.pytoptionsscKs#|jdt|jd||S(sSends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPRG(RR;R|(R<R_R((s5/usr/lib/python2.7/site-packages/requests/sessions.pytheadscKs|jd|d|d||S(sSends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. RIRR(R|(R<R_RRR((s5/usr/lib/python2.7/site-packages/requests/sessions.pytposts cKs|jd|d||S(s7Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. tPUTR(R|(R<R_RR((s5/usr/lib/python2.7/site-packages/requests/sessions.pytputscKs|jd|d||S(s9Sends a PATCH request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. tPATCHR(R|(R<R_RR((s5/usr/lib/python2.7/site-packages/requests/sessions.pytpatchscKs|jd||S(sSends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. tDELETE(R|(R<R_R((s5/usr/lib/python2.7/site-packages/requests/sessions.pytdelete'sc Ksq|jd|j|jd|j|jd|j|jd|jt|tsjtdnt}xT|j |j kr|j |j |j j |j }||krPn||_ qvW|j dt}|j d}|j d}|j d}|j d} |j d} |j} |jd|j } tj} | j||}tj| |_td | ||}|jrx-|jD]}t|j|j|jqWnt|j||j|j||d|d|d|d| d| }|r)g|D]}|^qng}|r]|jd ||j }||_n|sm|jn|S( sSend a given PreparedRequest.RKRMRNROs#You can only send PreparedRequests.RPRLR_R/i(RRKRMRNROR&R t ValueErrortsetR_RctaddR0tpopR8Rt get_adapterRtutcnowRqtelapsedRRURRmR|RXRztinsertRV(R<R|Rt checked_urlsR>RPRKRLRMRNRORtadaptertstarttrRrtgenRU((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRq0sV        %   c Cs|jrt|pi}x*|jD]\}}|j||q(W|tks`|dkrtjjdptjjd}qnt ||j }t ||j }t ||j }t ||j }i|d6|d6|d6|d6S(s6Check the environment and merge it with some settings.tREQUESTS_CA_BUNDLEtCURL_CA_BUNDLERMRORKRNN(R}RR(RR8R"tostenvironR0R.RORKRMRN( R<R_RORKRMRNt env_proxiesR$R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRs cCsMx6|jjD]%\}}|jj|r|SqWtd|dS(s>Returns the appropriate connnection adapter for the given URL.s*No connection adapters were found for '%s'N(RR(tlowerR^R(R<R_tprefixR((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRscCs(x!|jjD]}|jqWdS(s+Closes all adapters and as such the sessionN(RtvaluesR[(R<R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR[scCso||j|sRc(R)t __attrs__Rc(R<tstate((R<s5/usr/lib/python2.7/site-packages/requests/sessions.pyt __getstate__scCs||jdi}x*|jD]\}}t|||qWtt|_x'|jD]\}}||j| s. """"( !