U o a@sddlmZddlZddlZddlZddlZddlmZm Z ddlZddl m Z m Z mZmZmZmZmZmZmZmZmZmZmZddlmZddlmZddlmZdd lm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&dd l'm(Z(dd l)m*Z*dd l+m,Z,dd l-m.Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4ddl5m6Z6m7Z7m8Z8m9Z:m;Z;ddlj?Z?e@eAZBeCZDGdddeCZEejFejGhZHGdddeEe(ZIGdddeIZJddZKddZ9dS))absolute_importN)errortimeout) ClosedPoolError ProtocolErrorEmptyPoolErrorHeaderParsingErrorHostChangedErrorLocationValueError MaxRetryError ProxyErrorReadTimeoutErrorSSLError TimeoutErrorInsecureRequestWarningNewConnectionError)CertificateError)six)queue)port_by_schemeDummyConnectionHTTPConnectionHTTPSConnectionVerifiedHTTPSConnection HTTPException BaseSSLError)RequestMethods) HTTPResponse)is_connection_dropped)set_file_position)assert_header_parsing)Retry)Timeout)get_host parse_urlUrl_normalize_host_encode_target) LifoQueuec@sBeZdZdZdZeZd ddZddZddZ d d Z d d Z dS)ConnectionPoolzz Base class for all connection pools, such as :class:`.HTTPConnectionPool` and :class:`.HTTPSConnectionPool`. NcCs0|s tdt||jd|_||_||_dS)NzNo host specified.scheme)r r'r,hostlower _proxy_hostport)selfr-r0r2cC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-6mt8ur68\pip\_vendor\urllib3\connectionpool.py__init__Is  zConnectionPool.__init__cCsdt|j|j|jfS)Nz%s(host=%r, port=%r))type__name__r-r0r1r2r2r3__str__QszConnectionPool.__str__cCs|SNr2r7r2r2r3 __enter__TszConnectionPool.__enter__cCs |dS)NF)close)r1exc_typeexc_valexc_tbr2r2r3__exit__WszConnectionPool.__exit__cCsdS)D Close all pooled connections and disable the pool. Nr2r7r2r2r3r;\szConnectionPool.close)N) r6 __module__ __qualname____doc__r,r)QueueClsr4r8r:r?r;r2r2r2r3r*@s r*c @seZdZdZdZeZeZdde j ddddddf ddZ dd Z d!d d Z d d ZddZddZddZddZedfddZddZddZddZdddddeddddf dd ZdS)"HTTPConnectionPoolaN Thread-safe connection pool for one host. :param host: Host used for this HTTP Connection (e.g. "localhost"), passed into :class:`httplib.HTTPConnection`. :param port: Port used for this HTTP Connection (None is equivalent to 80), passed into :class:`httplib.HTTPConnection`. :param strict: Causes BadStatusLine to be raised if the status line can't be parsed as a valid HTTP/1.0 or 1.1 status line, passed into :class:`httplib.HTTPConnection`. .. note:: Only works in Python 2. This parameter is ignored in Python 3. :param timeout: Socket timeout in seconds for each individual connection. This can be a float or integer, which sets the timeout for the HTTP request, or an instance of :class:`urllib3.util.Timeout` which gives you more fine-grained control over request timeouts. After the constructor has been parsed, this is always a `urllib3.util.Timeout` object. :param maxsize: Number of connections to save that can be reused. More than 1 is useful in multithreaded situations. If ``block`` is set to False, more connections will be created but they will not be saved once they've been used. :param block: If set to True, no more than ``maxsize`` connections will be used at a time. When no free connections are available, the call will block until a connection has been released. This is a useful side effect for particular multithreaded situations where one does not want to use more than maxsize connections per host to prevent flooding. :param headers: Headers to include with all requests, unless other headers are given explicitly. :param retries: Retry configuration to use by default with requests in this pool. :param _proxy: Parsed proxy URL, should not be used directly, instead, see :class:`urllib3.connectionpool.ProxyManager`" :param _proxy_headers: A dictionary with proxy headers, should not be used directly, instead, see :class:`urllib3.connectionpool.ProxyManager`" :param \**conn_kw: Additional parameters are used to create fresh :class:`urllib3.connection.HTTPConnection`, :class:`urllib3.connection.HTTPSConnection` instances. httpNFrc Kst|||t||||_t|ts4t|}|dkrBtj}||_ ||_ | ||_ ||_ | |_| pli|_t|D]} |j dqxd|_d|_| |_|jr|jdgdS)Nrsocket_options)r*r4rstrict isinstancer# from_floatr"DEFAULTrretriesrDpoolblockproxy proxy_headersxrangeputnum_connections num_requestsconn_kw setdefault) r1r-r0rHrmaxsizerNheadersrL_proxy_proxy_headersrU_r2r2r3r4s(      zHTTPConnectionPool.__init__cCsR|jd7_td|j|j|jp$d|jf|j|j|jj|jd|j }|S)z9 Return a fresh :class:`HTTPConnection`. rz(Starting new HTTP connection (%d): %s:%s80)r-r0rrH) rSlogdebugr-r0 ConnectionClsrconnect_timeoutrHrUr1connr2r2r3 _new_conns zHTTPConnectionPool._new_conncCsd}z|jj|j|d}WnBtk r8t|dYn&tjk r\|jrXt|dYnX|rt|rt d|j | t |dddkrd}|p|S) a Get a connection. Will return a pooled connection if one is available. If no connections are available and :prop:`.block` is ``False``, then a fresh connection is returned. :param timeout: Seconds to wait before giving up and raising :class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and :prop:`.block` is ``True``. N)rNrzPool is closed.z>Pool reached maximum size and no more connections are allowed.z Resetting dropped connection: %s auto_openrr)rMgetrNAttributeErrorrrEmptyrrr]r^r-r;getattrrc)r1rrbr2r2r3 _get_conns$  zHTTPConnectionPool._get_conncCs^z|jj|ddWdStk r*Yn$tjk rLtd|jYnX|rZ|dS)a Put a connection back into the pool. :param conn: Connection object for the current host and port as returned by :meth:`._new_conn` or :meth:`._get_conn`. If the pool is already full, the connection is closed and discarded because we exceeded maxsize. If connections are discarded frequently, then maxsize should be increased. If the pool is closed, then the connection will be closed and discarded. FrNNz2Connection pool is full, discarding connection: %s) rMrRrfrFullr]warningr-r;rar2r2r3 _put_connszHTTPConnectionPool._put_conncCsdS)U Called right before a request is made, after the socket is created. Nr2rar2r2r3_validate_conn0sz!HTTPConnectionPool._validate_conncCsdSr9r2rar2r2r3_prepare_proxy6sz!HTTPConnectionPool._prepare_proxycCs2|tkr|jSt|tr$|St|SdS)z< Helper that always returns a :class:`urllib3.util.Timeout` N)_DefaultrclonerIr#rJ)r1rr2r2r3 _get_timeout:s   zHTTPConnectionPool._get_timeoutcCsjt|trt||d|t|dr>|jtkr>t||d|dt|ksVdt|krft||d|dS)zAIs the error actually a timeout? Will raise a ReadTimeout or pass!Read timed out. (read timeout=%s)errnoz timed outzdid not complete (read)N)rI SocketTimeoutrhasattrru_blocking_errnosstr)r1errurl timeout_valuer2r2r3_raise_timeoutFs( z!HTTPConnectionPool._raise_timeoutc Ks|jd7_||}||j|_z||Wn:ttfk rp}z|j|||jdW5d}~XYnX|r|j ||f|n|j ||f||j } t |ddr| dkrt ||d| | tjkr|jtn |j| zjz|jdd} WnTtk rPz |} Wn0tk rJ}zt|dW5d}~XYnXYnXWn<tttfk r}z|j||| dW5d}~XYnXt |d d } td |j|j|j||| | j| j zt!| j"Wn@t#tfk r} ztj$d |%|| dd W5d} ~ XYnX| S)a Perform a request on a given urllib connection object taken from our pool. :param conn: a connection from one of our connection pools :param timeout: Socket timeout in seconds for the request. This can be a float or integer, which will set the same timeout value for the socket connect and the socket read, or an instance of :class:`urllib3.util.Timeout`, which gives you more fine-grained control over your timeouts. r)rzr{r|NsockrrtT) buffering _http_vsn_strzHTTP/?z%s://%s:%s "%s %s %s" %s %sz$Failed to parse headers (url=%s): %s)exc_info)&rTrs start_connectr`rrorvrr}request_chunkedrequest read_timeoutrhrr#DEFAULT_TIMEOUTr~ settimeoutsocketgetdefaulttimeout getresponse TypeError BaseExceptionr raise_from SocketErrorr]r^r,r-r0statuslengthr!msgr rl _absolute_url) r1rbmethodr{rchunkedhttplib_request_kw timeout_objerhttplib_response http_versionhper2r2r3 _make_request_sp     (  z HTTPConnectionPool._make_requestcCst|j|j|j|djS)N)r,r-r0path)r&r,r-r0r{)r1rr2r2r3rsz HTTPConnectionPool._absolute_urlcCsV|jdkrdS|jd}|_z|jdd}|r|qWntjk rPYnXdS)r@NFrj)rMrer;rrg)r1old_poolrbr2r2r3r;s  zHTTPConnectionPool.closecCsx|drdSt|\}}}|dk r0t||d}|jrF|sFt|}n|js^|t|kr^d}|||f|j|j|jfkS)zj Check if the given ``url`` is a member of the same host as this connection pool. /TNr+) startswithr$r'r0rrer,r-)r1r{r,r-r0r2r2r3 is_same_hosts    zHTTPConnectionPool.is_same_hostTc Ks|dkr|j}t|ts*tj|||jd}| dkr>| dd} |rX||sXt||||drrt t |}nt t |j }d}| }|jdkr|}||jd}d}t|| } ztz||}|j| d}|j|_|jdk ot|d d }|r|||j||||||| d }| s(|nd}|| d <|jj|f|||d | }d}Wntj k rxt!|d Ynt"t#t$t%t&t't(fk r,}zd}t|t&t(frt'|}n>t|t$t)fr|jrt*d|}nt|t$t#frt%d|}|j+||||t,-dd}|.|}W5d}~XYnXW5|sJ|oD|}d}|rZ||X|st/0d||||j1|||||||f|| | | | d| Sdd}|o|2}|rV|j3dkrd}z|j+||||d}Wn,t4k r |j5r|||YSX|||6|t/7d|||j1||||f||||| | | | d| St8|9d}|:||j3|rz|j+||||d}Wn,t4k r|j;r|||YSX|||.|t/7d||j1||||f||||| | | | d| S|S)a Get a connection from the pool and perform an HTTP request. This is the lowest level call for making a request, so you'll need to specify all the raw details. .. note:: More commonly, it's appropriate to use a convenience method provided by :class:`.RequestMethods`, such as :meth:`request`. .. note:: `release_conn` will only behave as expected if `preload_content=False` because we want to make `preload_content=False` the default behaviour someday soon without breaking backwards compatibility. :param method: HTTP request method (such as GET, POST, PUT, etc.) :param body: Data to send in the request body (useful for creating POST requests, see HTTPConnectionPool.post_url for more convenience). :param headers: Dictionary of custom headers to send, such as User-Agent, If-None-Match, etc. If None, pool headers are used. If provided, these headers completely replace any pool-specific headers. :param retries: Configure the number of retries to allow before raising a :class:`~urllib3.exceptions.MaxRetryError` exception. Pass ``None`` to retry until you receive a response. Pass a :class:`~urllib3.util.retry.Retry` object for fine-grained control over different types of retries. Pass an integer number to retry connection errors that many times, but no other types of errors. Pass zero to never retry. If ``False``, then retries are disabled and any exception is raised immediately. Also, instead of raising a MaxRetryError on redirects, the redirect response will be returned. :type retries: :class:`~urllib3.util.retry.Retry`, False, or an int. :param redirect: If True, automatically handle redirects (status codes 301, 302, 303, 307, 308). Each redirect counts as a retry. Disabling retries will disable redirect, too. :param assert_same_host: If ``True``, will make sure that the host of the pool requests is consistent else will raise HostChangedError. When False, you can use the pool on an HTTP proxy and request foreign hosts. :param timeout: If specified, overrides the default timeout for this one request. It may be a float (in seconds) or an instance of :class:`urllib3.util.Timeout`. :param pool_timeout: If set and the pool is set to block=True, then this method will block for ``pool_timeout`` seconds and raise EmptyPoolError if no connection is available within the time period. :param release_conn: If False, then the urlopen call will not release the connection back into the pool once a response is received (but will release if you read the entire contents of the response such as when `preload_content=True`). This is useful if you're not preloading the response's content immediately. You will need to call ``r.release_conn()`` on the response ``r`` to return the connection back into the pool. If None, it takes the value of ``response_kw.get('preload_content', True)``. :param chunked: If True, urllib3 will send the body using chunked transfer encoding. Otherwise, urllib3 will send the body using the standard content-length form. Defaults to False. :param int body_pos: Position to seek to in file-like body in the event of a retry or redirect. Typically this won't need to be set because urllib3 will auto-populate the value when needed. :param \**response_kw: Additional parameters are passed to :meth:`urllib3.response.HTTPResponse.from_httplib` N)redirectdefaultpreload_contentTrrFF)rr~)rbodyrXrrequest_method)rM connectionrLz"No pool connections are available.zCannot connect to proxy.zConnection aborted.)r_pool _stacktracez1Retrying (%r) after connection broken by '%r': %s)r pool_timeout release_connrbody_posc Ss2z |Wn ttttttfk r,YnXdSr9)readrrrrrr)responser2r2r3drain_and_release_conns  z:HTTPConnectionPool.urlopen..drain_and_release_conni/GET)rrzRedirecting %s -> %s)rLrassert_same_hostrrrrrz Retry-Afterz Retry: %s)>> conn = connection_from_url('http://google.com/') >>> r = conn.request('GET', '/') Prr0N)r$rrerrE)r{kwr,r-r0r2r2r3connection_from_urls rcCs.t||}|dr*|dr*|dd}|S)z? Normalize hosts for comparisons and use with sockets. []r)normalize_hostrendswith)r-r,r2r2r3r' s  r')L __future__rruloggingrrrrrrrv exceptionsrrrr r r r r rrrrrZpackages.ssl_match_hostnamerpackagesrZpackages.six.movesrrrrrrrrrrrrrZutil.connectionrZ util.requestr Z util.responser!Z util.retryr"Z util.timeoutr#Zutil.urlr$r%r&r'rr(Z util.queuer)movesrQ getLoggerr6r]objectrqr*EAGAIN EWOULDBLOCKrxrErrr2r2r2r3sF <   $         $ r