
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
»nNjGC  ã               @   s   d dl mZ d dlZd dlZd dlZddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ dddgZejeƒZd6Zd7Zej d/eƒZ!d0d1„ Z"ej#e"e!ƒej#e"e!ƒd2œZ$e	e
d2œZ%G d3d„ deƒZ&G d4d„ de&ƒZ'd5d„ Z(dS )8é    )Úabsolute_importNé   )ÚHTTPHeaderDictÚRecentlyUsedContainer)ÚHTTPConnectionPoolÚHTTPSConnectionPool)Úport_by_scheme)ÚLocationValueErrorÚMaxRetryErrorÚProxySchemeUnknown)Úsix)Ú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Úkey_server_hostnameÚPoolKeyc             C   sÎ   |j ƒ }|d jƒ |d< |d jƒ |d< x4d	D ],}||kr.|| dk	r.t|| jƒ ƒ||< q.W |jdƒ}|dk	r|t|ƒ|d< x&t|jƒ ƒD ]}|j|ƒ|d| < qŠW x| j	D ]}||kr¬d||< q¬W | f |Ž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_optionsNZsocket_optionsZkey_)r6   r7   r8   )
ÚcopyÚlowerÚ	frozensetÚitemsÚgetÚtupleÚlistÚkeysÚpopÚ_fields)Z	key_classÚrequest_contextÚcontextÚkeyZsocket_optsZfield© rF   ú!/usr/lib/python3.6/poolmanager.pyÚ_default_key_normalizer;   s    

rH   )ÚhttpÚhttpsc               @   sx   e Zd ZdZdZddd„Zdd„ Zdd	„ Zdd
d„Zdd„ Z	ddd„Z
dd„ Zddd„Zd dd„Zdd„ Zd!dd„ZdS )"r   a$  
    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é
   c             K   s8   t j| |ƒ || _t|dd„ d�| _t| _tjƒ | _d S )Nc             S   s   | j ƒ S )N)Úclose)ÚprF   rF   rG   Ú<lambda>�   s    z&PoolManager.__init__.<locals>.<lambda>)Zdispose_func)r   Ú__init__Úconnection_pool_kwr   ÚpoolsÚpool_classes_by_schemeÚkey_fn_by_schemer9   )ÚselfÚ	num_poolsr6   rP   rF   rF   rG   rO   ™   s    zPoolManager.__init__c             C   s   | S )NrF   )rT   rF   rF   rG   Ú	__enter__¤   s    zPoolManager.__enter__c             C   s   | j ƒ  dS )NF)Úclear)rT   Úexc_typeZexc_valZexc_tbrF   rF   rG   Ú__exit__§   s    zPoolManager.__exit__c             C   sf   | j | }|dkr| jjƒ }xdD ]}|j|dƒ q"W |dkrXxtD ]}|j|dƒ qDW |||f|Ž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.
        Nr4   r5   ÚportrI   )r4   r5   rZ   )rR   rP   r9   rA   ÚSSL_KEYWORDS)rT   r4   r5   rZ   rC   Zpool_clsrE   ÚkwrF   rF   rG   Ú	_new_pool¬   s    




zPoolManager._new_poolc             C   s   | j jƒ  dS )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)rQ   rW   )rT   rF   rF   rG   rW   Ç   s    zPoolManager.clearrI   c             C   sT   |st dƒ‚| j|ƒ}|pd|d< |s:tj|d jƒ 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.
        zNo host specified.rI   r4   éP   rZ   r5   )r	   Ú_merge_pool_kwargsr   r=   r:   Úconnection_from_context)rT   r5   rZ   r4   Úpool_kwargsrC   rF   rF   rG   Úconnection_from_hostÐ   s    
z PoolManager.connection_from_hostc             C   s,   |d j ƒ }| 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.
        r4   )rC   )r:   rS   Úconnection_from_pool_key)rT   rC   r4   Zpool_key_constructorÚpool_keyrF   rF   rG   r`   ç   s    
z#PoolManager.connection_from_contextc             C   s`   | j j�N | j j|ƒ}|r|S |d }|d }|d }| j||||d�}|| j |< W dQ R X |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.
        r4   r5   rZ   )rC   N)rQ   Úlockr=   r]   )rT   rd   rC   Zpoolr4   r5   rZ   rF   rF   rG   rc   ô   s    
z$PoolManager.connection_from_pool_keyc             C   s    t |ƒ}| j|j|j|j|d�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.
        )rZ   r4   ra   )r   rb   r5   rZ   r4   )rT   Úurlra   ÚurF   rF   rG   Úconnection_from_url  s    zPoolManager.connection_from_urlc             C   sZ   | j jƒ }|rVxF|jƒ D ]:\}}|dkrJy
||= W qR tk
rF   Y qRX q|||< 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)rP   r9   r<   ÚKeyError)rT   ÚoverrideZbase_pool_kwargsrE   ÚvaluerF   rF   rG   r_     s    

zPoolManager._merge_pool_kwargsTc             K   s¢  t |ƒ}| j|j|j|jd�}d|d< d|d< d|krD| jjƒ |d< | jdk	rj|jdkrj|j||f|Ž}n|j||j	f|Ž}|o†|j
ƒ }|s�|S t||ƒ}|jdkrÄd	}d|d
< t|d ƒjƒ |d< |jdƒ}	t|	tƒsætj|	|d�}	|	joö|j|ƒ �r<ttj|d ƒƒ}
x.|
D ]&}|jƒ |	jk�r|d j|dƒ �qW y|	j||||d�}	W n  tk
�rr   |	j�rn‚ |S X |	|d< ||d< tjd||ƒ | j||f|Ž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.
        )rZ   r4   FZassert_same_hostÚredirectr6   NrI   i/  ZGETZbodyÚretries)rl   )ÚresponseZ_poolzRedirecting %s -> %s)r   rb   r5   rZ   r4   r6   r9   ÚproxyÚurlopenZrequest_uriZget_redirect_locationr   Zstatusr   Z_prepare_for_method_changer=   Ú
isinstancer   Zfrom_intZremove_headers_on_redirectZis_same_hostr?   r   Ziterkeysr:   rA   Z	incrementr
   Zraise_on_redirectÚlogÚinfo)rT   Úmethodrf   rl   r\   rg   Zconnrn   Zredirect_locationrm   r6   ÚheaderrF   rF   rG   rp   /  sH    	




zPoolManager.urlopen)rK   N)N)NrI   N)N)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ro   rO   rV   rY   r]   rW   rb   r`   rc   rh   r_   rp   rF   rF   rF   rG   r   {   s   

	


c                   sH   e Zd ZdZd‡ fdd„	Zd‡ fdd„	Zdd	d
„Zd‡ fdd„	Z‡  ZS )r   ax  
    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

    rK   Nc                s˜   t |tƒrd|j|j|jf }t|ƒ}|jsFtj|jdƒ}|j|d�}|jdkrZt	|jƒ‚|| _
|pfi | _| j
|d< | j|d< tt| ƒj||f|Ž d S )	Nz
%s://%s:%ir^   )rZ   rI   rJ   Ú_proxyr7   )rI   rJ   )rq   r   r4   r5   rZ   r   r   r=   Ú_replacer   ro   Úproxy_headersÚsuperr   rO   )rT   Ú	proxy_urlrU   r6   r|   rP   ro   rZ   )Ú	__class__rF   rG   rO   ‹  s    








zProxyManager.__init__rI   c                sD   |dkr t t| ƒj||||d�S t t| ƒj| jj| jj| jj|d�S )NrJ   )ra   )r}   r   rb   ro   r5   rZ   r4   )rT   r5   rZ   r4   ra   )r   rF   rG   rb   ¢  s
    

z!ProxyManager.connection_from_hostc             C   s0   ddi}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.
        ZAcceptz*/*ZHost)r   ÚnetlocÚupdate)rT   rf   r6   Zheaders_r€   rF   rF   rG   Ú_set_proxy_headersª  s    

zProxyManager._set_proxy_headersTc                sN   t |ƒ}|jdkr0|jd| jƒ}| j||ƒ|d< tt| ƒj||fd|i|—ŽS )z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rI   r6   rl   )r   r4   r=   r6   r‚   r}   r   rp   )rT   rt   rf   rl   r\   rg   r6   )r   rF   rG   rp   ¹  s
    
zProxyManager.urlopen)rK   NN)NrI   N)N)T)	rv   rw   rx   ry   rO   rb   r‚   rp   Ú__classcell__rF   rF   )r   rG   r   p  s    
c             K   s   t f d| i|—ŽS )Nr~   )r   )rf   r\   rF   rF   rG   r   Ç  s    )r   r   r   r   r   r   r   )r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   ))Z
__future__r   ÚcollectionsÚ	functoolsZloggingÚ_collectionsr   r   Zconnectionpoolr   r   r   Ú
exceptionsr	   r
   r   Zpackagesr   Zpackages.six.moves.urllib.parser   Zrequestr   Zutil.urlr   Z
util.retryr   Ú__all__Z	getLoggerrv   rr   r[   Z_key_fieldsÚ
namedtupler3   rH   ÚpartialrS   rR   r   r   r   rF   rF   rF   rG   Ú<module>   sd   

                        6
 vW