K i/*dZddlZddlmZddlZddlZddlmZddlm Z ddlm Z ddl m Z ddl mZdd lmZmZmZmZmZmZdd lmZddlZej0r dd lmZdd lmZd edefdZGddZ e jBZ"y)aWSGI support for the Tornado web framework. WSGI is the Python standard for web servers, and allows for interoperability between Tornado and other Python web frameworks and servers. This module provides WSGI support via the `WSGIContainer` class, which makes it possible to run applications using other WSGI frameworks on the Tornado HTTP server. The reverse is not supported; the Tornado `.Application` and `.RequestHandler` classes are designed for use with the Tornado `.HTTPServer` and cannot be used in a generic WSGI container. N)BytesIO)dummy_executor)escape)httputil)IOLoop) access_log)ListTupleOptionalCallableAnyDict) TracebackType)Type)WSGIApplicationsreturncHt|tsJ|jdS)Nlatin1) isinstancebytesdecode)rs R/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/wsgi.py to_wsgi_strr6s! a   88H ceZdZdZ ddddeej jddfdZde jddfd Z de jddfd Z de jde eeffd Zd ede jddfd Zy) WSGIContainera Makes a WSGI-compatible application runnable on Tornado's HTTP server. .. warning:: WSGI is a *synchronous* interface, while Tornado's concurrency model is based on single-threaded *asynchronous* execution. Many of Tornado's distinguishing features are not available in WSGI mode, including efficient long-polling and websockets. The primary purpose of `WSGIContainer` is to support both WSGI applications and native Tornado ``RequestHandlers`` in a single process. WSGI-only applications are likely to be better off with a dedicated WSGI server such as ``gunicorn`` or ``uwsgi``. Wrap a WSGI application in a `WSGIContainer` to make it implement the Tornado `.HTTPServer` ``request_callback`` interface. The `WSGIContainer` object can then be passed to classes from the `tornado.routing` module, `tornado.web.FallbackHandler`, or to `.HTTPServer` directly. This class is intended to let other frameworks (Django, Flask, etc) run on the Tornado HTTP server and I/O loop. Realistic usage will be more complicated, but the simplest possible example uses a hand-written WSGI application with `.HTTPServer`:: def simple_app(environ, start_response): status = "200 OK" response_headers = [("Content-type", "text/plain")] start_response(status, response_headers) return [b"Hello world!\n"] async def main(): container = tornado.wsgi.WSGIContainer(simple_app) http_server = tornado.httpserver.HTTPServer(container) http_server.listen(8888) await asyncio.Event().wait() asyncio.run(main()) The recommended pattern is to use the `tornado.routing` module to set up routing rules between your WSGI application and, typically, a `tornado.web.Application`. Alternatively, `tornado.web.Application` can be used as the top-level router and `tornado.web.FallbackHandler` can embed a `WSGIContainer` within it. If the ``executor`` argument is provided, the WSGI application will be executed on that executor. This must be an instance of `concurrent.futures.Executor`, typically a ``ThreadPoolExecutor`` (``ProcessPoolExecutor`` is not supported). If no ``executor`` is given, the application will run on the event loop thread in Tornado 6.3; this will change to use an internal thread pool by default in Tornado 7.0. .. warning:: By default, the WSGI application is executed on the event loop's thread. This limits the server to one request at a time (per process), making it less scalable than most other WSGI servers. It is therefore highly recommended that you pass a ``ThreadPoolExecutor`` when constructing the `WSGIContainer`, after verifying that your application is thread-safe. The default will change to use a ``ThreadPoolExecutor`` in Tornado 7.0. .. versionadded:: 6.3 The ``executor`` parameter. .. deprecated:: 6.3 The default behavior of running the WSGI application on the event loop thread is deprecated and will change in Tornado 7.0 to use a thread pool by default. Nwsgi_application WSGIAppTypeexecutorrc<||_| t|_y||_yN)rrr )selfrr s r__init__zWSGIContainer.__init__}s !1*2*:  rrequestc`tjj|j|yr")rcurrentspawn_callbackhandle_request)r#r%s r__call__zWSGIContainer.__call__s''(;(;WErc Kig ddtdttttfdttdtttt fdt tgtfffd }tj}|j|j|j|j||d{} t|dttffd } |j|j|d{}|nj!|9 t#|dr|j% d j'}s t)d dj+d d \}} t-|} d} | D chc]\} } | j/}} } t1j2|}| d k7r>d|vr%| j!dtt5|fd|vr| j!dd|vr$| j!ddt6j8zft;j<d| | }t;j>}| D]\}}|jA|||jBJ|jBjE||||jBjG|jI| |y77#t#|dr|j%wwxYwcc} } ww)Nstatusheadersexc_infozOptional[Type[BaseException]]rc0|d<|d<jS)Nr,r-)append)r,r-r.dataresponses rstart_responsez4WSGIContainer.handle_request..start_responses!$DN%DO?? "rc: tS#t$rYywxYwr")next StopIteration)app_response_itersr next_chunkz0WSGIContainer.handle_request..next_chunks(  122$   s  closerz$WSGI app did not call start_response i0zcontent-lengthContent-Lengthz content-type) Content-Typeztext/html; charset=UTF-8serverServerzTornadoServer/%szHTTP/1.1)chunkr")%strr r r BaseExceptionrr rr rr'run_in_executorr renvironiterr0hasattrr9join Exceptionsplitintlowerrutf8lentornadoversionrResponseStartLine HTTPHeadersadd connection write_headersfinish_log)r#r%r3loop app_responser8r@bodystatus_code_strreason status_coder-kv header_set start_line header_objkeyvaluer7r1r2s @@@rr)zWSGIContainer.handle_requests # #%S/* #3]+]+- #ugsl # #~~!11 MM  ! ! LL !     % $\ 2   "224==*MM=& |W-""$xx!BC C"&x."6"6sA">/* y/.56FQaggi6 6{{4  # z1 0#c$i.ABZ/KL : % NNH&87??&JK L// KP ))+ ! 'JC NN3 & '!!---((Zt(L!!# +w'c $N |W-""$.7sJB9K0>K?K0>KKKA(K0K*D$K0KK'' K0cr|jjd}t|dk(r|d}t|d}n|j}|jdk(rdnd}|j dt tj|jd d |j|j|t||jd |jttj|j t"j$|j&t(ud d d}d|j*vr|j*j-d|d<d|j*vr|j*j-d|d<|j*j/D]+\}}||d|j1ddj3z<-|S)zConverts a `tornado.httputil.HTTPServerRequest` to a WSGI environment. .. versionchanged:: 6.3 No longer a static method. :rr;httpsiPNF)encodingplus)r;rT)REQUEST_METHOD SCRIPT_NAME PATH_INFO QUERY_STRING REMOTE_ADDR SERVER_NAME SERVER_PORTSERVER_PROTOCOLz wsgi.versionzwsgi.url_schemez wsgi.inputz wsgi.errorszwsgi.multithreadzwsgi.multiprocessz wsgi.run_oncer= CONTENT_TYPEr<CONTENT_LENGTHHTTP_-_)hostrIrMrJprotocolmethodrr url_unescapepathquery remote_iprArOrrLrYsysstderrr rr-popitemsreplaceupper)r#r%hostportryportrDrbrcs rrDzWSGIContainer.environs| <<%%c* x=A A;Dx{#D<;Frr")__name__ __module__ __qualname____doc__r concurrentfuturesExecutorr$rHTTPServerRequestr*r)rrAr rDrJrVrrrr;s?H;?I'I:--667I  IF : :FtFE(H,F,FE(4E(N&x99&d38n&PGGh.H.HGTGrr)#rconcurrent.futuresriorrNrtornado.concurrentrrrtornado.ioloopr tornado.logrtypingr r r r r rtypesr TYPE_CHECKINGr_typeshed.wsgirrrrArrr HTTPRequestrrrrsq  -!"==  =5S NGNGb(( r