Your IP : 216.73.216.213


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



[�A�@s�ddlmZddlZddlZddlZddlmZddlmZm	Z	ddlm
Z
ddlmZm
Z
mZddlmZdd	lmZdd
lmZddlmZdd
dgZeje�Zd5Zd6Zejd-e�Zd.d/�Zd0ej ee�d1ej ee�iZ!d0ed1e	iZ"Gd2d�de�Z#Gd3d
�d
e#�Z$d4d�Z%dS)7�)�absolute_importN�)�RecentlyUsedContainer)�HTTPConnectionPool�HTTPSConnectionPool)�port_by_scheme)�LocationValueError�
MaxRetryError�ProxySchemeUnknown)�urljoin)�RequestMethods)�	parse_url)�Retry�PoolManager�ProxyManager�proxy_from_url�key_file�	cert_file�	cert_reqs�ca_certs�ssl_version�ca_cert_dir�ssl_context�
key_scheme�key_host�key_port�key_timeout�key_retries�
key_strict�	key_block�key_source_address�key_key_file�
key_cert_file�
key_cert_reqs�key_ca_certs�key_ssl_version�key_ca_cert_dir�key_ssl_context�key_maxsize�key_headers�
key__proxy�key__proxy_headers�key_socket_options�key__socks_options�key_assert_hostname�key_assert_fingerprint�PoolKeycCs|j�}|dj�|d<|dj�|d<xDd	D]<}||kr;||dk	r;t||j��||<q;W|jd�}|dk	r�t|�|d<x1t|j��D]}|j|�|d|<q�Wx'|j	D]}||kr�d||<q�W||�S)
a�
    Create a pool key out of a request context dictionary.

    According to RFC 3986, both the scheme and host are case-insensitive.
    Therefore, this function normalizes both before constructing the pool
    key for an HTTPS request. If you wish to change this behaviour, provide
    alternate callables to ``key_fn_by_scheme``.

    :param key_class:
        The class to use when constructing the key. This should be a namedtuple
        with the ``scheme`` and ``host`` keys at a minimum.
    :type  key_class: namedtuple
    :param request_context:
        A dictionary-like object that contain the context for a request.
    :type  request_context: dict

    :return: A namedtuple that can be used as a connection pool key.
    :rtype:  PoolKey
    �scheme�host�headers�_proxy_headers�_socks_optionsNZsocket_optionsZkey_)r3r4r5)
�copy�lower�	frozenset�items�get�tuple�list�keys�pop�_fields)Z	key_class�request_context�context�keyZsocket_opts�field�rD�D/opt/alt/python35/lib/python3.5/site-packages/urllib3/poolmanager.py�_default_key_normalizer9s
rF�http�httpsc@s�eZdZdZdZdddd�Zdd�Zdd	�Zdd
d�Zdd
�Z	ddddd�Z
dd�Zddd�Zddd�Z
dd�Zddd�ZdS)ra$
    Allows for arbitrary requests while transparently keeping track of
    necessary connection pools for you.

    :param num_pools:
        Number of connection pools to cache before discarding the least
        recently used pool.

    :param headers:
        Headers to include with all requests, unless other headers are given
        explicitly.

    :param \**connection_pool_kw:
        Additional parameters are used to create fresh
        :class:`urllib3.connectionpool.ConnectionPool` instances.

    Example::

        >>> manager = PoolManager(num_pools=2)
        >>> r = manager.request('GET', 'http://google.com/')
        >>> r = manager.request('GET', 'http://google.com/mail')
        >>> r = manager.request('GET', 'http://yahoo.com/')
        >>> len(manager.pools)
        2

    N�
cKsPtj||�||_t|ddd��|_t|_tj�|_dS)NZdispose_funccSs
|j�S)N)�close)�prDrDrE�<lambda>�sz&PoolManager.__init__.<locals>.<lambda>)r�__init__�connection_pool_kwr�pools�pool_classes_by_scheme�key_fn_by_schemer6)�self�	num_poolsr3rNrDrDrErM�s			zPoolManager.__init__cCs|S)NrD)rRrDrDrE�	__enter__�szPoolManager.__enter__cCs|j�dS)NF)�clear)rR�exc_typeZexc_val�exc_tbrDrDrE�__exit__�s
zPoolManager.__exit__cCs�|j|}|dkr(|jj�}xdD]}|j|d�q/W|dkrvxtD]}|j|d�q\W||||�S)a�
        Create a new :class:`ConnectionPool` based on host, port, scheme, and
        any additional pool keyword arguments.

        If ``request_context`` is provided, it is provided as keyword arguments
        to the pool class used. This method is used to actually create the
        connection pools handed out by :meth:`connection_from_url` and
        companion methods. It is intended to be overridden for customization.
        Nr1r2�portrG)r1r2rY)rPrNr6r>�SSL_KEYWORDS)rRr1r2rYr@Zpool_clsrB�kwrDrDrE�	_new_pool�s



zPoolManager._new_poolcCs|jj�dS)z�
        Empty our store of pools and direct them all to close.

        This will not affect in-flight connections, but they will not be
        re-used after completion.
        N)rOrU)rRrDrDrErU�szPoolManager.clearrGcCst|std��|j|�}|p*d|d<|sStj|dj�d�}||d<||d<|j|�S)a�
        Get a :class:`ConnectionPool` based on the host, port, and scheme.

        If ``port`` isn't given, it will be derived from the ``scheme`` using
        ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
        provided, it is merged with the instance's ``connection_pool_kw``
        variable and used to create the new connection pool, if one is
        needed.
        zNo host specified.rGr1�PrYr2)r�_merge_pool_kwargsrr:r7�connection_from_context)rRr2rYr1�pool_kwargsr@rDrDrE�connection_from_host�s

z PoolManager.connection_from_hostcCs<|dj�}|j|}||�}|j|d|�S)z�
        Get a :class:`ConnectionPool` based on the request context.

        ``request_context`` must at least contain the ``scheme`` key and its
        value must be a key in ``key_fn_by_scheme`` instance variable.
        r1r@)r7rQ�connection_from_pool_key)rRr@r1Zpool_key_constructor�pool_keyrDrDrEr_�s
z#PoolManager.connection_from_contextc
Csz|jj�g|jj|�}|r)|S|d}|d}|d}|j|||d|�}||j|<WdQRX|S)z�
        Get a :class:`ConnectionPool` based on the provided pool key.

        ``pool_key`` should be a namedtuple that only contains immutable
        objects. At a minimum it must have the ``scheme``, ``host``, and
        ``port`` fields.
        r1r2rYr@N)rO�lockr:r\)rRrcr@�poolr1r2rYrDrDrErb�s



z$PoolManager.connection_from_pool_keycCs4t|�}|j|jd|jd|jd|�S)a�
        Similar to :func:`urllib3.connectionpool.connection_from_url`.

        If ``pool_kwargs`` is not provided and a new pool needs to be
        constructed, ``self.connection_pool_kw`` is used to initialize
        the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
        is provided, it is used instead. Note that if a new pool does not
        need to be created for the request, the provided ``pool_kwargs`` are
        not used.
        rYr1r`)r
rar2rYr1)rR�urlr`�urDrDrE�connection_from_url
s!zPoolManager.connection_from_urlcCso|jj�}|rkxS|j�D]E\}}|dkr]y||=Wqgtk
rYYqgXq"|||<q"W|S)a
        Merge a dictionary of override values for self.connection_pool_kw.

        This does not modify self.connection_pool_kw and returns a new dict.
        Any keys in the override dictionary with a value of ``None`` are
        removed from the merged dictionary.
        N)rNr6r9�KeyError)rR�overrideZbase_pool_kwargsrB�valuerDrDrEr^s
zPoolManager._merge_pool_kwargsTc
Ks�t|�}|j|jd|jd|j�}d|d<d|d<d|krc|jj�|d<|jdk	r�|jdkr�|j|||�}n|j||j	|�}|o�|j
�}|s�|St||�}|jd	kr�d
}|j
d�}	t|	t�s$tj|	d|�}	|	jre|j|�rex%|	jD]}
|dj|
d�qGWy"|	j||d|d
|�}	Wn"tk
r�|	jr��|SYnX|	|d<||d<tjd||�|j|||�S)a]
        Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        rYr1FZassert_same_host�redirectr3NrGi/ZGET�retries�responseZ_poolzRedirecting %s -> %s)r
rar2rYr1r3r6�proxy�urlopenZrequest_uriZget_redirect_locationr�statusr:�
isinstancerZfrom_intZremove_headers_on_redirectZis_same_hostr>Z	incrementr	Zraise_on_redirect�log�info)rR�methodrfrlr[rgZconnrnZredirect_locationrm�headerrDrDrErp-s@	$

	"
		

zPoolManager.urlopen)�__name__�
__module__�__qualname__�__doc__rorMrTrXr\rUrar_rbrhr^rprDrDrDrErys	
cspeZdZdZddd�fdd�Zddd�fdd�Zdd	d
�Zd�fdd
�Z�S)rax
    Behaves just like :class:`PoolManager`, but sends all requests through
    the defined proxy, using the CONNECT method for HTTPS URLs.

    :param proxy_url:
        The URL of the proxy to be used.

    :param proxy_headers:
        A dictionary containing headers that will be sent to the proxy. In case
        of HTTP they are being sent with each request, while in the
        HTTPS/CONNECT case they are sent only once. Could be used for proxy
        authentication.

    Example:
        >>> proxy = urllib3.ProxyManager('http://localhost:3128/')
        >>> r1 = proxy.request('GET', 'http://google.com/')
        >>> r2 = proxy.request('GET', 'http://httpbin.org/')
        >>> len(proxy.pools)
        1
        >>> r3 = proxy.request('GET', 'https://httpbin.org/')
        >>> r4 = proxy.request('GET', 'https://twitter.com/')
        >>> len(proxy.pools)
        3

    rINcs�t|t�r+d|j|j|jf}t|�}|jsgtj|jd�}|jd|�}|jdkr�t	|j��||_
|p�i|_|j
|d<|j|d<tt
|�j|||�dS)	Nz
%s://%s:%ir]rYrGrH�_proxyr4)rGrH)rrrr1r2rYr
rr:�_replacer
ro�
proxy_headers�superrrM)rR�	proxy_urlrSr3r}rNrorY)�	__class__rDrErM�s
		

zProxyManager.__init__rGcsb|dkr.tt|�j|||d|�Stt|�j|jj|jj|jjd|�S)NrHr`)r~rraror2rYr1)rRr2rYr1r`)r�rDrEra�s
z!ProxyManager.connection_from_hostcCsBddi}t|�j}|r+||d<|r>|j|�|S)z�
        Sets headers needed by proxies: specifically, the Accept and Host
        headers. Only sets headers not provided by the user.
        ZAcceptz*/*ZHost)r
�netloc�update)rRrfr3Zheaders_r�rDrDrE�_set_proxy_headers�s

zProxyManager._set_proxy_headersTcsht|�}|jdkrF|jd|j�}|j||�|d<tt|�j||d||�S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rGr3rl)r
r1r:r3r�r~rrp)rRrurfrlr[rgr3)r�rDrErp�s
zProxyManager.urlopen)rwrxryrzrMrar�rprDrD)r�rEriscKstd||�S)Nr)r)rfr[rDrDrEr�s)rrrrrrr)rrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/)&�
__future__r�collections�	functoolsZlogging�_collectionsrZconnectionpoolrrr�
exceptionsrr	r
Zpackages.six.moves.urllib.parser�requestrZutil.urlr
Z
util.retryr�__all__Z	getLoggerrwrsrZZ_key_fields�
namedtupler0rF�partialrQrPrrrrDrDrDrE�<module>s^6�W