Current File : //lib/python2.7/site-packages/requests/sessions.pyc
�
ZЅ_c@s�dZddlZddlmZddlmZddlmZddlmZm	Z	m
Z
mZddlm
Z
mZmZmZdd	lmZmZmZdd
lmZmZddlmZmZmZmZdd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	cookielibtOrderedDictturljointurlparse(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(tHTTPAdapter(trequote_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	<genexpr>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.pytmerge_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<n|jtjkr�|
dkr�d}
n|jtjkr�|
dkr�d}
n|jtj kr|
d	krd}
n|
|
_|jtj!tj"fkrRd
|
jkrF|
jd
=nd|
_$n|
j}y|d=Wnt%k
rynXt&|
j'||j	�|
j'j(|j)�|
j*|
j'�|j+|
|�}|j,|
|�|
}|j-|d|d
|d|d|d|dt�}t&|j)|
|j	�|d7}|VqWdS(s6Receives a Response. Returns a generator of Responses.itdecode_contentsExceeded %s redirects.tlocations//s%s:%stHEADtGETtPOSTsContent-LengthtCookietstreamttimeouttverifytcerttproxiestallow_redirectsiN(.tis_redirecttcopytappendtlistthistorytcontentRRtRuntimeErrortrawtreadR;t
max_redirectsRtclosetheaderstmethodt
startswithRturlR9tgeturltnetlocRRRtis_permanent_redirecttredirect_cachetstatus_codeR t	see_othertfoundtmovedttemporary_redirecttpermanent_redirectR"tbodytKeyErrorRt_cookiesR'tcookiestprepare_cookiestrebuild_proxiestrebuild_authtsend(R<tresptreqRKRLRMRNROtithisttprepared_requesttnew_histR_R]tparsed_rurltparsedR\((s5/usr/lib/python2.7/site-packages/requests/sessions.pytresolve_redirectsvs|


						
	
	
cCs{|j}|j}d|kr@|j|jj|�r@|d=n|jrUt|�nd}|dk	rw|j|�ndS(s�
        When being redirected we may want to strip authentication from the
        request to avoid leaking credentials. This method intelligently removes
        and reapplies authentication where possible to avoid credential loss.
        t
AuthorizationN(R\R_RDtrequestt	trust_envRR"tprepare_auth(R<RvR/R\R_tnew_auth((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRp�s		$
cCs|j}|j}t|�j}|dk	r9|j�ni}|jr�t|�r�t|�}|j	|�}|r�|j
|||�q�nd|kr�|d=nyt||�\}	}
Wntk
r�d\}	}
nX|	r|
rt
|	|
�|d<n|S(s�
        This method re-evaluates the proxy configuration by considering the
        environment variables. If we are redirected to a URL covered by
        NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
        proxy keys for this URL (in case they were stripped by a previous
        redirect).

        This method also replaces the Proxy-Authorization header where
        necessary.
        sProxy-AuthorizationN(NN(R\R_RR9R"RRR}RRR0t
setdefaultRRkR(R<RvROR\R_R9tnew_proxiestenviron_proxiestproxytusernametpassword((s5/usr/lib/python2.7/site-packages/requests/sessions.pyRo�s$		

N(	t__name__t
__module__RDR;R"R8RzRpRo(((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR4[s
	n	tSessioncBseZdZddddddddd	d
ddd
g
Zd�Zd�Zd�Zd�Zd!d!d!d!d!d!d!e	d!d!d!d!d!d!d�Z
d�Zd�Zd�Z
d!d!d�Zd!d�Zd!d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd �ZRS("s�A Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> 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}RRmRR�tmountRRtREDIRECT_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
|dt|j|j��
|S(sbConstructs a :class:`PreparedRequest <PreparedRequest>` for
        transmission and returns it. The :class:`PreparedRequest` has settings
        merged from the :class:`Request <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|	d6|
d6}|j|�|j||�}|S(
sCConstructs a :class:`Request <Request>`, prepares it and sends it.
        Returns :class:`Response <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 <user/advanced.html#timeouts>`_) 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(RRR�R�tmerge_environment_settingsR_R'Rq(R<R]R_R�R�R\RmR�R�RLRPROR�RKRMRNR�Rstpreptsettingstsend_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�tget_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�p�tjjd�}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_RORKRMRNtenv_proxiesR$R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR�s	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(R�R(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(R�tvaluesR[(R<R%((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR[�scCso||j|<g|jD]$}t|�t|�kr|^q}x'|D]}|jj|�|j|<qHWdS(skRegisters a connection adapter to a prefix.

        Adapters are sorted in descending order by key length.N(R�tlenR�(R<R�R�R$tkeys_to_movetkey((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR��s
4
cs6t�fd��jD��}t�j�|d<|S(Nc3s'|]}|t�|d�fVqdS(N(tgetattrR"(R#tattr(R<(s5/usr/lib/python2.7/site-packages/requests/sessions.pys	<genexpr>�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|<q[WdS(NRc(R�R(tsetattrRR�Rc(R<R�RcR�tvaluetredirecttto((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt__setstate__�sN(R�R�t__doc__R�R�R�R�R�R"R8R|R0R�R�R�R�R�R�RqR�R�R[R�R�R�(((s5/usr/lib/python2.7/site-packages/requests/sessions.pyR� sH		6			)E	
	
	


			O		
			cCst�S(s2Returns a :class:`Session` for context-management.(R�(((s5/usr/lib/python2.7/site-packages/requests/sessions.pytsession�s(7R�R�tcollectionsRRR�RtcompatRRRRRmRRR	R
tmodelsRRR
R�RRtutilsRRRRt
exceptionsRRRRturllib3._collectionsRt
structuresRR�RRRRRRtstatus_codesR R!R�R.R3tobjectR4R�R�(((s5/usr/lib/python2.7/site-packages/requests/sessions.pyt<module>
s.""""(!���