Your IP : 216.73.216.196


Current Path : /opt/alt/python35/lib/python3.5/site-packages/requests/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/requests/__pycache__/sessions.cpython-35.pyc



m"[�l�@s"dZddlZddlZddlZddlmZddlmZddlm	Z	m
Z
mZmZm
Z
mZddlmZmZmZmZddlmZmZmZdd	lmZmZdd
lmZddlmZmZddl m!Z!m"Z"m#Z#m$Z$dd
l%m&Z&ddl'm(Z(ddlm)Z)m*Z*m+Z+m,Z,m-Z-m.Z.ddl/m0Z0ddlm1Z1ej2dkr�y
ej3Z4Wne5k
r�ej6Z4YnXn	ejZ4edd�Z7edd�Z8Gdd�de9�Z:Gdd�de:�Z;dd�Z<dS)z�
requests.session
~~~~~~~~~~~~~~~~

This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
�N)�	timedelta�)�_basic_auth_str)�	cookielib�is_py3�OrderedDict�urljoin�urlparse�Mapping)�cookiejar_from_dict�extract_cookies_to_jar�RequestsCookieJar�
merge_cookies)�Request�PreparedRequest�DEFAULT_REDIRECT_LIMIT)�
default_hooks�
dispatch_hook)�to_native_string)�to_key_val_list�default_headers)�TooManyRedirects�
InvalidSchema�ChunkedEncodingError�ContentDecodingError)�CaseInsensitiveDict)�HTTPAdapter)�requote_uri�get_environ_proxies�get_netrc_auth�should_bypass_proxies�get_auth_from_url�rewind_body)�codes)�REDIRECT_STATI�win32cCs�|dkr|S|dkr |St|t�o;t|t�sB|S|t|��}|jt|��dd�|j�D�}x|D]
}||=q�W|S)z�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`
    NcSs(g|]\}}|dkr|�qS)N�)�.0�k�vr&r&�B/opt/alt/python35/lib/python3.5/site-packages/requests/sessions.py�
<listcomp>Is	z!merge_setting.<locals>.<listcomp>)�
isinstancer
r�update�items)Zrequest_settingZsession_setting�
dict_classZmerged_settingZ	none_keys�keyr&r&r*�
merge_setting1s
r1cCsZ|dks!|jd�gkr%|S|dksF|jd�gkrJ|St|||�S)z�Properly merges both requests and session hooks.

    This is necessary because when request_hooks == {'response': []}, the
    merge breaks Session hooks entirely.
    N�response)�getr1)Z
request_hooksZ
session_hooksr/r&r&r*�merge_hooksPs
!!r4c@s^eZdZdd�Zdddddddd�Zdd	�Zd
d�Zdd
�ZdS)�SessionRedirectMixincCs<|jr8|jd}tr+|jd�}t|d�SdS)z7Receives a Response. Returns a redirect URI or ``None``�location�latin1�utf8N)Zis_redirect�headersr�encoder)�self�respr6r&r&r*�get_redirect_targetas	

z(SessionRedirectMixin.get_redirect_targetFNTc	ks2g}
|j|�}t|j�j}x|r-|j�}
|
j|�|
dd�|_y|jWn.tt	t
fk
r�|jjdd�YnXt
|j�|jkr�td|jd|��|j�|jd�r
t|j�}dt|j�|f}t|�}|jd	kr@|r@|jd
|�}n|jrR|j}|j�}|js�t|jt|��}nt|�}t|�|
_|j|
|�|jtjtjfkr�d}x!|D]}|
jj|d�q�Wd|
_ |
j}y|d=Wnt!k
r#YnXt"|
j#||j�t$|
j#|j%�|
j&|
j#�|j'|
|�}|j(|
|�|
j)dk	o�d|kp�d
|k}|r�t*|
�|
}|r�|Vq*|j+|d|d|d|d|d|dd|	�}t"|j%|
|j�|j|�}|Vq*WdS)zBReceives a Response. Returns a generator of Responses or Requests.rNZdecode_contentFzExceeded %s redirects.r2z//z%s:%s��fragment�Content-Length�Content-Type�Transfer-EncodingZCookie�stream�timeout�verify�cert�proxies�allow_redirects)r@rArB),r=r	�urlr?�copy�append�history�contentrr�RuntimeError�raw�read�len�
max_redirectsr�close�
startswithr�scheme�_replace�geturl�netlocrr�rebuild_method�status_coder#Ztemporary_redirectZpermanent_redirectr9�pop�body�KeyErrorrZ_cookiesr�cookiesZprepare_cookies�rebuild_proxies�rebuild_authZ_body_positionr"�send)r;r<�reqrCrDrErFrG�yield_requestsZadapter_kwargsZhistrIZprevious_fragment�prepared_requestZparsed_rurl�parsedZpurged_headers�headerr9Z
rewindabler&r&r*�resolve_redirectsvs|	

			
		

	z&SessionRedirectMixin.resolve_redirectscCs�|j}|j}d|krUt|jj�}t|�}|j|jkrU|d=|jrjt|�nd}|dk	r�|j|�dS)z�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.
        Z
AuthorizationN)r9rIr	�request�hostname�	trust_envrZprepare_auth)r;rdr2r9rIZoriginal_parsedZredirect_parsedZnew_authr&r&r*r`�s		
z!SessionRedirectMixin.rebuild_authc
Cs*|dk	r|ni}|j}|j}t|�j}|j�}|jd�}t|d|�}|jr�|r�t|d|�}	|	j||	jd��}
|
r�|j	||
�d|kr�|d=yt
||�\}}Wntk
rd\}}YnX|r&|r&t||�|d<|S)a�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.

        :rtype: dict
        N�no_proxy�allzProxy-Authorization)NN)
r9rIr	rUrJr3r rjr�
setdefaultr!r]r)
r;rdrGr9rIrUZnew_proxiesrkZbypass_proxyZenviron_proxies�proxy�username�passwordr&r&r*r_�s*		
z$SessionRedirectMixin.rebuild_proxiescCs�|j}|jtjkr-|dkr-d}|jtjkrQ|dkrQd}|jtjkru|dkrud}||_dS)z�When being redirected we may want to change the method of the request
        based on certain specs or browser behavior.
        �HEAD�GET�POSTN)�methodrZr#Z	see_other�foundZmoved)r;rdr2rtr&r&r*rY(s	z#SessionRedirectMixin.rebuild_method)�__name__�
__module__�__qualname__r=rgr`r_rYr&r&r&r*r5_sp)r5c@s]eZdZdZdddddddd	d
ddd
dg
Zdd�Zdd�Zdd�Zdd�Zdddddddddddddddd�Z	dd�Z
dd�Zdd �Zddd!d"�Z
dd#d$�Zdd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�ZdS)7�Sessiona~A Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> import requests
      >>> s = requests.Session()
      >>> s.get('http://httpbin.org/get')
      <Response [200]>

    Or as a context manager::

      >>> with requests.Session() as s:
      >>>     s.get('http://httpbin.org/get')
      <Response [200]>
    r9r^�authrG�hooks�paramsrErFZprefetch�adaptersrCrjrRcCs�t�|_d|_i|_t�|_i|_d|_d|_d|_	t
|_d|_t
i�|_t�|_|jdt��|jdt��dS)NFTzhttps://zhttp://)rr9rzrGrr{r|rCrErFrrRrjrr^rr}�mountr)r;r&r&r*�__init__Xs								zSession.__init__cCs|S)Nr&)r;r&r&r*�	__enter__�szSession.__enter__cGs|j�dS)N)rS)r;�argsr&r&r*�__exit__�szSession.__exit__cCs$|jpi}t|tj�s-t|�}ttt�|j�|�}|j}|jr}|r}|jr}t	|j
�}t�}|jd|j
j�d|j
d|jd|jd|jdt|j|jdt�dt|j|j�d	t||j�d
|dt|j|j��
|S)a�Constructs 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.
        :rtype: requests.PreparedRequest
        rtrI�files�data�jsonr9r/r|rzr^r{)r^r,rZ	CookieJarrrr
rzrjrrIrZpreparert�upperr�r�r�r1r9rr|r4r{)r;rhr^Zmerged_cookiesrz�pr&r&r*�prepare_request�s*
							zSession.prepare_requestNTcCs�td|j�d|d|d|d|p-id|d|p?id|d	|d
|�
}|j|�}|poi}|j|j||
||�}d|	d|
i}|j|�|j||�}|S)
a�Constructs 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, 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 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 :ref:`(connect timeout,
            read timeout) <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 or protocol and
            hostname to the URL of the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :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. Defaults to ``True``.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        :rtype: requests.Response
        rtrIr9r�r�r�r|rzr^r{rDrH)rr�r��merge_environment_settingsrIr-ra)r;rtrIr|r�r9r^r�rzrDrHrGr{rCrErFr�rbZprepZsettingsZsend_kwargsr<r&r&r*rh�s()	
zSession.requestcKs#|jdd�|jd||�S)z�Sends a GET request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        rHTrr)rmrh)r;rI�kwargsr&r&r*r3szSession.getcKs#|jdd�|jd||�S)z�Sends a OPTIONS request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        rHTZOPTIONS)rmrh)r;rIr�r&r&r*�optionsszSession.optionscKs#|jdd�|jd||�S)z�Sends a HEAD request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        rHFrq)rmrh)r;rIr�r&r&r*�headszSession.headcKs|jd|d|d||�S)a�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.
        :rtype: requests.Response
        rsr�r�)rh)r;rIr�r�r�r&r&r*�post%s
zSession.postcKs|jd|d||�S)aYSends 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.
        :rtype: requests.Response
        ZPUTr�)rh)r;rIr�r�r&r&r*�put1s	zSession.putcKs|jd|d||�S)a[Sends 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.
        :rtype: requests.Response
        ZPATCHr�)rh)r;rIr�r�r&r&r*�patch<s	z
Session.patchcKs|jd||�S)z�Sends a DELETE request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        ZDELETE)rh)r;rIr�r&r&r*�deleteGszSession.deletec

Ks�|jd|j�|jd|j�|jd|j�|jd|j�t|t�rgtd��|jdd�}|j	d�}|j
}|jd|j�}t
�}|j||�}t
�|}	td	|	�|_td
|||�}|jr+x*|jD]}
t|j|
j|
j�qWt|j||j�|j|||�}|rodd�|D�ng}|r�|jd
|�|j�}||_|s�y(t|j||dd|��|_Wntk
r�YnX|s�|j|S)zISend a given PreparedRequest.

        :rtype: requests.Response
        rCrErFrGz#You can only send PreparedRequests.rHTrI�secondsr2cSsg|]}|�qSr&r&)r'r<r&r&r*r+�s	z Session.send.<locals>.<listcomp>rrc)rmrCrErFrGr,r�
ValueErrorr[r3r{�get_adapterrI�preferred_clockrar�elapsedrrLrr^rhrOrg�insert�nextZ_next�
StopIterationrM)
r;rhr�rHrCr{�adapter�start�rr�r<�genrLr&r&r*raQsB		
		(
zSession.sendc
Cs	|jr�|dk	r$|jd�nd}t|d|�}x*|j�D]\}}	|j||	�qIW|dks�|dkr�tjjd�p�tjjd�}t||j�}t||j	�}t||j
�}t||j�}d|d|d|d	|iS)
z^
        Check the environment and merge it with some settings.

        :rtype: dict
        NrkTZREQUESTS_CA_BUNDLEZCURL_CA_BUNDLErErGrCrF)rjr3rr.rm�os�environr1rGrCrErF)
r;rIrGrCrErFrkZenv_proxiesr(r)r&r&r*r��s	!z"Session.merge_environment_settingscCsSx<|jj�D]+\}}|j�j|j��r|SqWtd|��dS)z~
        Returns the appropriate connection adapter for the given URL.

        :rtype: requests.adapters.BaseAdapter
        z*No connection adapters were found for '%s'N)r}r.�lowerrTr)r;rI�prefixr�r&r&r*r��szSession.get_adaptercCs(x!|jj�D]}|j�qWdS)z+Closes all adapters and as such the sessionN)r}�valuesrS)r;r)r&r&r*rS�sz
Session.closecsW||j�<�fdd�|jD�}x'|D]}|jj|�|j|<q0WdS)zwRegisters a connection adapter to a prefix.

        Adapters are sorted in descending order by prefix length.
        cs.g|]$}t|�t��kr|�qSr&)rQ)r'r()r�r&r*r+�s	z!Session.mount.<locals>.<listcomp>N)r}r[)r;r�r�Zkeys_to_mover0r&)r�r*r~�s

z
Session.mountcs&t�fdd��jD��}|S)Nc3s'|]}|t�|d�fVqdS)N)�getattr)r'�attr)r;r&r*�	<genexpr>�sz'Session.__getstate__.<locals>.<genexpr>)�dict�	__attrs__)r;�stater&)r;r*�__getstate__�s"zSession.__getstate__cCs1x*|j�D]\}}t|||�q
WdS)N)r.�setattr)r;r�r��valuer&r&r*�__setstate__�szSession.__setstate__)rvrwrx�__doc__r�rr�r�r�rhr3r�r�r�r�r�r�rar�r�rSr~r�r�r&r&r&r*ry?s2	7)D
IrycCst�S)zQ
    Returns a :class:`Session` for context-management.

    :rtype: Session
    )ryr&r&r&r*�session�sr�)=r�r��sys�time�datetimerrzr�compatrrrrr	r
r^rrr
rZmodelsrrrr{rrZ_internal_utilsr�utilsrr�
exceptionsrrrrZ
structuresrr}rrrrr r!r"Zstatus_codesr#r$�platform�perf_counterr��AttributeError�clockr1r4�objectr5ryr�r&r&r&r*�<module>	s:."".

	���