ó ZÐ…_c@sËdZddlZddlmZddlmZddlmZddlmZm 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 d„Z1e d„Z2de3fd„ƒYZ4de4fd„ƒYZ5d„Z6dS(s” requests.session ~~~~~~~~~~~~~~~~ This module provides a Session object to manage and persist settings across requests (cookies, auth, proxies). iÿÿÿÿN(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_STATIiècCsº|dkr|S|dkr |St|tƒo;t|tƒsB|S|t|ƒƒ}|jt|ƒƒx0|jƒD]"\}}|dkrt||=qtqtWtd„|jƒDƒƒ}|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!|jdƒgkr%|S|dksF|jdƒgkrJ|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;eZd„Zededdd„Zd„Zd„ZRS(cCsàt|ƒ}t|ƒ}|j|jkr.tS|jdkrn|jdkrn|jdkrn|jdkrntS|j|jk}|j|jk}tj|jdƒdf}| rÖ|j|krÖ|j|krÖtS|pß|S(sFDecide whether Authorization header should be removed when redirectingthttpiPthttpsi»N(iPN(i»N( 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 krµt d|j ƒ‚n|jƒ|jd} |j} | jdƒr 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}RZcCs´tƒ|_d|_i|_tƒ|_i|_t|_ t |_ d|_ t |_t |_tiƒ|_tƒ|_|jdtƒƒ|jdtƒƒttƒ|_dS(Nshttps://shttp://(RR\R"R‰RORRŠR‹R;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|jƒdS(N(R[(R<targs((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt__exit__lscCs*|jp i}t|tjƒs0t|ƒ}ntttƒ|jƒ|ƒ}|j}|jrƒ| rƒ|j rƒt |j ƒ}nt ƒ}|j d|j jƒd|j d|jd|jd|jdt|j|jdtƒdt|j|jƒd t||jƒd |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,R‹R‰RmRŠ(RmR&Rt CookieJarRR R R‰R}RR_R tprepareR]tupperR”R•R–R.R\RR‹R3RŠ(R<R|Rmtmerged_cookiesR‰tp((s5/usr/lib/python2.7/site-packages/requests/sessions.pytprepare_requestos*        cCsÓt|ƒ}td|jƒd|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\R”R•R–R‹R‰RmRŠRLRP(RR R™Rœtmerge_environment_settingsR_R'Rq(R<R]R_R‹R•R\RmR”R‰RLRPRORŠRKRMRNR–Rstpreptsettingst send_kwargsRr((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR|—s,2        cKs#|jdtƒ|jd||S(sÃSends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPRH(R€R8R|(R<R_tkwargs((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR0êscKs#|jdtƒ|jd||S(sÇSends a OPTIONS request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPtOPTIONS(R€R8R|(R<R_R¡((s5/usr/lib/python2.7/site-packages/requests/sessions.pytoptionsôscKs#|jdtƒ|jd||S(sÄSends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. RPRG(R€R;R|(R<R_R¡((s5/usr/lib/python2.7/site-packages/requests/sessions.pytheadþscKs|jd|d|d||S(sŠSends 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. RIR•R–(R|(R<R_R•R–R¡((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_R•R¡((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_R•R¡((s5/usr/lib/python2.7/site-packages/requests/sessions.pytpatchscKs|jd||S(sÆSends 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|jƒt|tƒsjtdƒ‚ntƒ}xT|j |j krÉ|j |j ƒ|j j |j ƒ}||kr½Pn||_ qvW|j dtƒ}|j dƒ}|j dƒ}|j dƒ}|j dƒ} |j dƒ} |j} |jd|j ƒ} tjƒ} | j||}tjƒ| |_td | ||}|jrÄx-|jD]}t|j|j|jƒqžWnt|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(R€RKRMRNROR&R t ValueErrortsetR_RctaddR0tpopR8RŠt get_adapterRtutcnowRqtelapsedRRURRmR|RXRztinsertRV(R<R|R¡t checked_urlsR>RPRKRLRMRNRORŠtadaptertstarttrRrtgenRU((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRq0sV        %   c Csò|jrŠt|ƒpi}x*|jƒD]\}}|j||ƒq(W|tks`|dkrŠtjjdƒptjjdƒ}qŠnt ||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(R€R8R"tostenvironR0R.RORKRMRN( R<R_RORKRMRNt env_proxiesR$R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRs cCsMx6|jjƒD]%\}}|jƒj|ƒ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.pyR°—scCs(x!|jjƒD]}|jƒqWdS(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*|jƒD]\}}t|||ƒqWttƒ|_x'|jƒD]\}}||j| s. """"( ! Åÿ¡