K ic|NdZddlZddlZddlmZddlZddlZddlZddlm Z m Z m Z ddl m Z mZddlmZmZddlmZddlmZdd lmZmZmZmZmZmZmZGd d ZGd d eZGddZ GddZ!Gdde"Z#e#Z$GddZ%ddZ&e'dk(re&yy)aBlocking and non-blocking HTTP client interfaces. This module defines a common interface shared by two implementations, ``simple_httpclient`` and ``curl_httpclient``. Applications may either instantiate their chosen implementation class directly or use the `AsyncHTTPClient` class from this module, which selects an implementation that can be overridden with the `AsyncHTTPClient.configure` method. The default implementation is ``simple_httpclient``, and this is expected to be suitable for most users' needs. However, some applications may wish to switch to ``curl_httpclient`` for reasons such as the following: * ``curl_httpclient`` has some features not found in ``simple_httpclient``, including support for HTTP proxies and the ability to use a specified network interface. * ``curl_httpclient`` is more likely to be compatible with sites that are not-quite-compliant with the HTTP spec, or sites that use little-exercised features of HTTP. * ``curl_httpclient`` is faster. Note that if you are using ``curl_httpclient``, it is highly recommended that you use a recent version of ``libcurl`` and ``pycurl``. Currently the minimum supported version of libcurl is 7.22.0, and the minimum version of pycurl is 7.18.2. It is highly recommended that your ``libcurl`` installation is built with asynchronous DNS resolver (threaded or c-ares), otherwise you may encounter various problems with request timeouts (for more information, see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS and comments in curl_httpclient.py). To select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup:: AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") N)BytesIO)Future"future_set_result_unless_cancelled%future_set_exception_unless_cancelled)utf8 native_str)genhttputil)IOLoop) Configurable)TypeAnyUnionDictCallableOptionalcastcVeZdZdZ ddddeddfdZddZdd Zd ed e fdedd fd Z y) HTTPClientaA blocking HTTP client. This interface is provided to make it easier to share code between synchronous and asynchronous applications. Applications that are running an `.IOLoop` must use `AsyncHTTPClient` instead. Typical usage looks like this:: http_client = httpclient.HTTPClient() try: response = http_client.fetch("http://www.google.com/") print(response.body) except httpclient.HTTPError as e: # HTTPError is raised for non-200 responses; the response # can be found in e.response. print("Error: " + str(e)) except Exception as e: # Other errors are possible, such as IOError. print("Error: " + str(e)) http_client.close() .. versionchanged:: 5.0 Due to limitations in `asyncio`, it is no longer possible to use the synchronous ``HTTPClient`` while an `.IOLoop` is running. Use `AsyncHTTPClient` instead. Nasync_client_classzOptional[Type[AsyncHTTPClient]]kwargsreturnc d|_td|_tdfd }|jj ||_d|_y)NTF) make_currentcbKtjdd{JdiS7w)Nr)r sleep)rrsX/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/httpclient.py make_clientz(HTTPClient.__init__..make_clienths7))A,  %1 11%// / s /-/)rAsyncHTTPClient)_closedr _io_loopr run_sync _async_client)selfrrrs `` r__init__zHTTPClient.__init__YsI E2  %!0  0 "]]33K@ c$|jyN)closer%s r__del__zHTTPClient.__del__ps  r'c|js<|jj|jjd|_yy)z2Closes the HTTPClient, freeing any resources used.TN)r!r$r*r"r+s rr*zHTTPClient.closess7||    $ $ & MM   !DLr'request HTTPRequest HTTPResponsec |jjtj|jj |fi|}|S)aExecutes a request, returning an `HTTPResponse`. The request may be either a string URL or an `HTTPRequest` object. If it is a string, we construct an `HTTPRequest` using any additional kwargs: ``HTTPRequest(request, **kwargs)`` If an error occurs during the fetch, we raise an `HTTPError` unless the ``raise_error`` keyword argument is set to False. )r"r# functoolspartialr$fetch)r%r.rresponses rr4zHTTPClient.fetchzs@==))   d0066 J6 J r'r)rN) __name__ __module__ __qualname____doc__rr&r,r*rstrr4rr'rrr;s`>AE=  . ]C/0rAs rconfigurable_defaultz$AsyncHTTPClient.configurable_defaults C$$r'cd|jz}t||st||tjt ||S)N_async_client_dict_)r7hasattrsetattrweakrefWeakKeyDictionarygetattr)r> attr_names r_async_clientszAsyncHTTPClient._async_clientss<)CLL8 sI& CG$=$=$? @sI&&r'force_instancerc tj}|rd}n|j}| ||vr||St||fi|}||_||||j <|Sr))r currentrLsuper__new___instance_cacheio_loop)r>rMrrSinstance_cacheinstance __class__s rrQzAsyncHTTPClient.__new__su.." !N //1N  %'^*C!'* *7?31&1 $2  %/7N8++ ,r'defaultsctj|_ttj |_||j j|d|_y)NF) r rOrSdictr/ _DEFAULTSrWupdater!)r%rWs r initializezAsyncHTTPClient.initializesA~~' [223   MM  * r'c|jryd|_|j9|jj|jd}|||ur t dyyy)aDestroys this HTTP client, freeing any file descriptors used. This method is **not needed in normal use** due to the way that `AsyncHTTPClient` objects are transparently reused. ``close()`` is generally only necessary when either the `.IOLoop` is also being closed, or the ``force_instance=True`` argument was used when creating the `AsyncHTTPClient`. No other methods may be called on the `AsyncHTTPClient` after ``close()``. NTz"inconsistent AsyncHTTPClient cache)r!rRpoprS RuntimeError)r% cached_vals rr*zAsyncHTTPClient.closesf <<     +--11$,,EJ %*D*@"#GHH+A% ,r'r.r/ raise_errorzFuture[HTTPResponse]c d|jr tdt|tstdd|i|}n |r t dt j |j|_t||j}tdfd }|jtt||S)aExecutes a request, asynchronously returning an `HTTPResponse`. The request may be either a string URL or an `HTTPRequest` object. If it is a string, we construct an `HTTPRequest` using any additional kwargs: ``HTTPRequest(request, **kwargs)`` This method returns a `.Future` whose result is an `HTTPResponse`. By default, the ``Future`` will raise an `HTTPError` if the request returned a non-200 response code (other errors may also be raised if the server could not be contacted). Instead, if ``raise_error`` is set to False, the response will always be returned regardless of the response code. If a ``callback`` is given, it will be invoked with the `HTTPResponse`. In the callback interface, `HTTPError` is not automatically raised. Instead, you must check the response's ``error`` attribute or call its `~HTTPResponse.rethrow` method. .. versionchanged:: 6.0 The ``callback`` argument was removed. Use the returned `.Future` instead. The ``raise_error=False`` argument only affects the `HTTPError` raised when a non-200 response code is used, instead of suppressing all errors. z(fetch() called on closed AsyncHTTPClienturlz8kwargs can't be used if request is an HTTPRequest objectc|jr%s |jst|jyt|yr))error_error_is_response_coderr)r5futureras rhandle_responsez.AsyncHTTPClient.fetch..handle_response+s0~~h&F&F9&(..Q .vx @r'r)r5r0rN) r!r_ isinstancer/ ValueErrorr HTTPHeadersheaders _RequestProxyrWr fetch_implr)r%r.rar request_proxyrhrgs ` @rr4zAsyncHTTPClient.fetchsD <<IJ J';/!8g88G N #..w?%gt}}=  A [-8/J r'callbackr0ctr))NotImplementedError)r%r.rps rrnzAsyncHTTPClient.fetch_impl5s "##r'implz$Union[None, str, Type[Configurable]]c &t||fi|y)aGConfigures the `AsyncHTTPClient` subclass to use. ``AsyncHTTPClient()`` actually creates an instance of a subclass. This method may be called with either a class object or the fully-qualified name of such a class (or ``None`` to use the default, ``SimpleAsyncHTTPClient``) If additional keyword arguments are given, they will be passed to the constructor of each subclass instance created. The keyword argument ``max_clients`` determines the maximum number of simultaneous `~AsyncHTTPClient.fetch()` operations that can execute in parallel on each `.IOLoop`. Additional arguments may be supported depending on the implementation class in use. Example:: AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") N)rP configure)r>rsrrVs rruzAsyncHTTPClient.configure:s, $)&)r')Fr)r6)T)r7r8r9r:rR classmethodr r r?rCrr rLboolrrQrr;r\r*rr4rrnru __classcell__rVs@rr r sF$LO$|"4%T,%7%% 'tF,=$=>'' TSEV$8DcN#;tI:!:sM)*:: : :x$$$08.9I49O0P$ $ *9*EH* **r'r cG<eZdZdZdZeddddddddZ d2d ed ed ee e eefe jfd ee e efd eedeedeedeedeedee eejfdeedeedeedeedeedeee gdfdeeegdfdeeegdfdeedeedeedeedeed eed!eed"eed#eed$eed%eed&eeee gdfgd'fd(ed)eed*ee e eefej*fd+dfDd,Zed+e jfd-Zej2d.e e eefe jfd+dfd/Zed+e fd0Zej2d.e e efd+dfd1Zy)3r/zHTTP client request object.Ng4@TF)connect_timeoutrequest_timeoutfollow_redirects max_redirectsdecompress_responseproxy_passwordallow_nonstandard_methods validate_certrcmethodrlbody auth_username auth_password auth_moder}r~if_modified_sincerr user_agentuse_gzipnetwork_interfacestreaming_callbackheader_callbackprepare_curl_callback proxy_host proxy_portproxy_usernamerproxy_auth_moderrca_certs allow_ipv6 client_key client_cert body_producerz Future[None]expect_100_continuer ssl_optionsrc"D||_| r"tj| |jd<||_||_||_||_||_||_||_ ||_ ||_ ||_ ||_ ||_||_| |_| |_| |_| |_| | |_n||_||_||_||_||_||_||_||_||_||_||_|!|_||_ tCjB|_"y)a%All parameters except ``url`` are optional. :arg str url: URL to fetch :arg str method: HTTP method, e.g. "GET" or "POST" :arg headers: Additional HTTP headers to pass on the request :type headers: `~tornado.httputil.HTTPHeaders` or `dict` :arg body: HTTP request body as a string (byte or unicode; if unicode the utf-8 encoding will be used) :type body: `str` or `bytes` :arg collections.abc.Callable body_producer: Callable used for lazy/asynchronous request bodies. It is called with one argument, a ``write`` function, and should return a `.Future`. It should call the write function with new data as it becomes available. The write function returns a `.Future` which can be used for flow control. Only one of ``body`` and ``body_producer`` may be specified. ``body_producer`` is not supported on ``curl_httpclient``. When using ``body_producer`` it is recommended to pass a ``Content-Length`` in the headers as otherwise chunked encoding will be used, and many servers do not support chunked encoding on requests. New in Tornado 4.0 :arg str auth_username: Username for HTTP authentication :arg str auth_password: Password for HTTP authentication :arg str auth_mode: Authentication mode; default is "basic". Allowed values are implementation-defined; ``curl_httpclient`` supports "basic" and "digest"; ``simple_httpclient`` only supports "basic" :arg float connect_timeout: Timeout for initial connection in seconds, default 20 seconds (0 means no timeout) :arg float request_timeout: Timeout for entire request in seconds, default 20 seconds (0 means no timeout) :arg if_modified_since: Timestamp for ``If-Modified-Since`` header :type if_modified_since: `datetime` or `float` :arg bool follow_redirects: Should redirects be followed automatically or return the 3xx response? Default True. :arg int max_redirects: Limit for ``follow_redirects``, default 5. :arg str user_agent: String to send as ``User-Agent`` header :arg bool decompress_response: Request a compressed response from the server and decompress it after downloading. Default is True. New in Tornado 4.0. :arg bool use_gzip: Deprecated alias for ``decompress_response`` since Tornado 4.0. :arg str network_interface: Network interface or source IP to use for request. See ``curl_httpclient`` note below. :arg collections.abc.Callable streaming_callback: If set, ``streaming_callback`` will be run with each chunk of data as it is received, and ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in the final response. :arg collections.abc.Callable header_callback: If set, ``header_callback`` will be run with each header line as it is received (including the first line, e.g. ``HTTP/1.0 200 OK\r\n``, and a final line containing only ``\r\n``. All lines include the trailing newline characters). ``HTTPResponse.headers`` will be empty in the final response. This is most useful in conjunction with ``streaming_callback``, because it's the only way to get access to header data while the request is in progress. :arg collections.abc.Callable prepare_curl_callback: If set, will be called with a ``pycurl.Curl`` object to allow the application to make additional ``setopt`` calls. :arg str proxy_host: HTTP proxy hostname. To use proxies, ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username``, ``proxy_pass`` and ``proxy_auth_mode`` are optional. Proxies are currently only supported with ``curl_httpclient``. :arg int proxy_port: HTTP proxy port :arg str proxy_username: HTTP proxy username :arg str proxy_password: HTTP proxy password :arg str proxy_auth_mode: HTTP proxy Authentication mode; default is "basic". supports "basic" and "digest" :arg bool allow_nonstandard_methods: Allow unknown values for ``method`` argument? Default is False. :arg bool validate_cert: For HTTPS requests, validate the server's certificate? Default is True. :arg str ca_certs: filename of CA certificates in PEM format, or None to use defaults. See note below when used with ``curl_httpclient``. :arg str client_key: Filename for client SSL key, if any. See note below when used with ``curl_httpclient``. :arg str client_cert: Filename for client SSL certificate, if any. See note below when used with ``curl_httpclient``. :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in ``simple_httpclient`` (unsupported by ``curl_httpclient``). Overrides ``validate_cert``, ``ca_certs``, ``client_key``, and ``client_cert``. :arg bool allow_ipv6: Use IPv6 when available? Default is True. :arg bool expect_100_continue: If true, send the ``Expect: 100-continue`` header and wait for a continue response before sending the request body. Only supported with ``simple_httpclient``. .. note:: When using ``curl_httpclient`` certain options may be inherited by subsequent fetches because ``pycurl`` does not allow them to be cleanly reset. This applies to the ``ca_certs``, ``client_key``, ``client_cert``, and ``network_interface`` arguments. If you use these options, you should pass them on every request (you don't have to always use the same values, but it's not possible to mix requests that specify these options with ones that use the defaults). .. versionadded:: 3.1 The ``auth_mode`` argument. .. versionadded:: 4.0 The ``body_producer`` and ``expect_100_continue`` arguments. .. versionadded:: 4.2 The ``ssl_options`` argument. .. versionadded:: 4.5 The ``proxy_auth_mode`` argument. zIf-Modified-SinceN)#rlr format_timestamprrrrrrcrrrrrrr}r~rrrrrrrrrrrrrrrrtime start_time)"r%rcrrlrrrrr}r~rrrrrrrrrrrrrrrrrrrrrrrrs" rr&zHTTPRequest.__init__fs/r 080I0I!1DLL, -%$,,.  ***".. 0*$  *':D $'/D $!2"4.%:")B&*  $$&&#6 ))+r'c|jSr))_headersr+s rrlzHTTPRequest.headers's }}r'valuecJ|tj|_y||_yr))r rkrr%rs rrlzHTTPRequest.headers.s =$002DM!DMr'c|jSr))_bodyr+s rrzHTTPRequest.body5s zzr'c$t||_yr))rrrs rrzHTTPRequest.body9s %[ r') GETNNNNNNNNNNNNNNNNNNNNNNNNNNNNFNN)r7r8r9r:rrYrZr;rrrr rkbytesfloatdatetimerwintrrssl SSLContextr&propertyrlsetterrrr'rr/r/Ss%H  "' IIM,0'+'+#'+/+/GK+/'+$(#'+/@D;?AE$($((,(,)-48(,"&%)$(%) $).2GKI& &&%S#X0D0D DEF & uUCZ() &  } & }&C=&"%&"%&$E%1B1B*B$CD&#4.& }&SM&4.& $C=!&"%Xugtm%<=#&$"(C5$;"78%&& (#(=>'&(SM)&*SM+&,! -&.! /&0"#1&2$,D>3&4 ~5&63-7&8TN9&:SM;&<c]=&> hw}-.> ? ?&D"E&F&d^G&HeDcNCNN$BCDI&J K&B--  ^^"U4S>83G3G#GH"T"" e [[!%s +!!!r'r/ceZdZdZdZdZdZ ddedede e jde e de e d e ed e ed e ee efd e e d e eddfdZedefdZddZde fdZy)r0aPHTTP Response object. Attributes: * ``request``: HTTPRequest object * ``code``: numeric HTTP status code, e.g. 200 or 404 * ``reason``: human-readable reason phrase describing the status code * ``headers``: `tornado.httputil.HTTPHeaders` object * ``effective_url``: final location of the resource after following any redirects * ``buffer``: ``cStringIO`` object for response body * ``body``: response body as bytes (created on demand from ``self.buffer``) * ``error``: Exception object, if any * ``request_time``: seconds from request start to finish. Includes all network operations from DNS resolution to receiving the last byte of data. Does not include time spent in the queue (due to the ``max_clients`` option). If redirects were followed, only includes the final request. * ``start_time``: Time at which the HTTP operation started, based on `time.time` (not the monotonic clock used by `.IOLoop.time`). May be ``None`` if the request timed out while in the queue. * ``time_info``: dictionary of diagnostic timing information from the request. Available data are subject to change, but currently uses timings available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html, plus ``queue``, which is the delay (if any) introduced by waiting for a slot under `AsyncHTTPClient`'s ``max_clients`` setting. .. versionadded:: 5.1 Added the ``start_time`` attribute. .. versionchanged:: 5.1 The ``request_time`` attribute previously included time spent in the queue for ``simple_httpclient``, but not in ``curl_httpclient``. Now queueing time is excluded in both implementations. ``request_time`` is now more accurate for ``curl_httpclient`` because it uses a monotonic clock when available. NFr.coderlbuffer effective_urlre request_time time_inforeasonrrc Jt|tr|j|_n||_||_| xs tj j |d|_|||_nt j|_||_ d|_ ||j|_ n||_ d|_|U|jdks|jdk\r/d|_t|j|j||_nd|_n||_| |_||_|xsi|_y)NUnknownFi,T)messager5)rirmr.rr responsesgetrrlrkrrrcrrf HTTPErrorrerrr) r%r.rrlrrrerrrrs rr&zHTTPResponse.__init__us g} -"??DL"DL G 2 2 6 6tY G  "DL#//1DL   !(D !.D ',$ =yy3$))s"2/3,&tyy$++PTU ! DJ$("br'c|jy|j|jj|_|jS)Nr')rrgetvaluer+s rrzHTTPResponse.bodys7 ;;  ZZ --/DJzzr'c4|jr |jy)z;If there was an error on the request, raise an `HTTPError`.N)rer+s rrethrowzHTTPResponse.rethrows ::**  r'cdjdt|jjD}|jj d|dS)N,c3&K|] }d|z yw)z%s=%rNr).0is r z(HTTPResponse.__repr__..sK! Ks())joinsorted__dict__itemsrVr7)r%argss r__repr__zHTTPResponse.__repr__sFxxKVDMM4G4G4I-JKK..))*!D633r')NNNNNNNNr6)r7r8r9r:rerfr.r/rrr rkrr; BaseExceptionrrr&rrrrrrr'rr0r0>s/d E#G 37$('+)-(,04 $&*()()()(../ () ! ()  } () &()uo()De,-() ()UO() ()Te 4#4r'r0c VeZdZdZ d dedeedeeddffd ZdefdZ e Z xZ S) HTTPClientErroraException thrown for an unsuccessful HTTP request. Attributes: * ``code`` - HTTP error integer error code, e.g. 404. Error code 599 is used when no HTTP response was received, e.g. for a timeout. * ``response`` - `HTTPResponse` object, if any. Note that if ``follow_redirects`` is False, redirects become HTTPErrors, and you can look at ``error.response.headers['Location']`` to see the destination of the redirect. .. versionchanged:: 5.1 Renamed from ``HTTPError`` to ``HTTPClientError`` to avoid collisions with `tornado.web.HTTPError`. The name ``tornado.httpclient.HTTPError`` remains as an alias. Nrrr5rc||_|xs tjj|d|_||_t ||||y)Nr)rr rrrr5rPr&)r%rrr5rVs rr&zHTTPClientError.__init__sC  I("4"4"8"8y"I    w1r'c8d|j|jfzS)Nz HTTP %d: %s)rrr+s r__str__zHTTPClientError.__str__s 4<<888r')NN) r7r8r9r:rrr;r0r&rrrxrys@rrrsW."&+/ 2 2# 2<( 2  299Hr'rcDeZdZdZdedeeeefddfdZ dedefdZ y) rmzoCombines an object with a dictionary of defaults. Used internally by AsyncHTTPClient implementations. r.rWrNc ||_||_yr))r.rW)r%r.rWs rr&z_RequestProxy.__init__s   r'namect|j|}||S|j|jj|dSyr))rJr.rWr)r%r request_attrs r __getattr__z_RequestProxy.__getattr__sBt||T2  #  ]] &==$$T40 0r') r7r8r9r:r/rrr;rr&rrr'rrmrmsD !"!.6tCH~.F! ! r'rmcddlm}m}m}|dtd|dtd|dtd|d td|d t |d t |}t}|D]} |j||j|j|j|j }|jrt!|j"|j$spt!t'|j(|j+y#t$r$}|j |j}nYd}~d}~wwxYw)Nr)defineoptionsparse_command_line print_headersF)typedefault print_bodyTrrr)rr)rrrr)tornado.optionsrrrrwr;rrr4rrrrrr5rprintrlrrrr*)rrrrclientargr5es rmainrsCC ?u5 D E D??E__main__r6)(r:rr2iorrrrHtornado.concurrentrrrtornado.escaperrtornador r tornado.ioloopr tornado.utilr typingr rrrrrrrr r/r0 Exceptionrrrmrr7rr'rrs$L  ,!!%CCCNNbD*lD*Nh!h!Vq4q4h'i'T  ,@ zFr'