Vc9h~dZddlZddlZddlZddlZddlZeedredz ZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd l mZdd lmZd ZdeddZdeddZeedrdeddZdeddZGdde jZGddee jZGddZGddZdS)) StreamReader StreamWriterStreamReaderProtocolopen_connection start_serverNAF_UNIX)open_unix_connectionstart_unix_server) coroutines)events) exceptions)format_helpers) protocols)logger)sleepi)limitc Ktj}t||}t|| |j fd||fi|d{V\}}t | ||}||fS)aA wrapper for create_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to create_connection() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). (If you want to customize the StreamReader and/or StreamReaderProtocol classes, just copy the code -- there's really nothing special here except some convenience.) rlooprcSNprotocols(/usr/lib64/python3.11/asyncio/streams.pyz!open_connection..1sN)r get_running_looprrcreate_connectionr) hostportrkwdsrreader transport_writerrs @rrrs&  " $ $D D 1 1 1F#F666H//$..(,........LIq )Xvt < rclKtjfd}j|||fi|d{VS)aStart a socket server, call back for each client connected. The first parameter, `client_connected_cb`, takes two parameters: client_reader, client_writer. client_reader is a StreamReader object, while client_writer is a StreamWriter object. This parameter can either be a plain callback function or a coroutine; if it is a coroutine, it will be automatically converted into a Task. The rest of the arguments are all the usual arguments to loop.create_server() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. The return value is the same as loop.create_server(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). The return value is the same as loop.create_server(), i.e. a Server object which can be used to stop the service. cNt}t|}|SNrrrrr%rclient_connected_cbrrs rfactoryzstart_server..factoryOs6E555'0C-1333rN)r r create_server)r.r"r#rr$r/rs` ` @rrr6so.  " $ $D $#GT4@@4@@ @ @ @ @ @ @@rcKtj}t||}t|||jfd|fi|d{V\}}t |||}||fS)z@Similar to `open_connection` but works with UNIX Domain Sockets.rrcSrrrsrrz&open_unix_connection..csHrN)r r rrcreate_unix_connectionr) pathrr$rr%r&r'r(rs @rr r [s&((E555'T:::8T8    d,,&*,,,,,,,, 1i64@@v~rcjKtjfd}j||fi|d{VS)z=Similar to `start_server` but works with UNIX Domain Sockets.cNt}t|}|Sr+r,r-s rr/z"start_unix_server..factoryls6!D999F+F4G15777HOrN)r r create_unix_server)r.r4rr$r/rs` ` @rr r gsm&((        -T,WdCCdCCCCCCCCCrc8eZdZdZd dZdZdZdZdZdZ dS) FlowControlMixina)Reusable flow control logic for StreamWriter.drain(). This implements the protocol methods pause_writing(), resume_writing() and connection_lost(). If the subclass overrides these it must call the super methods. StreamWriter.drain() must wait for _drain_helper() coroutine. Nc|tjd|_n||_d|_t j|_d|_dS)N) stacklevelF)r _get_event_loop_loop_paused collectionsdeque_drain_waiters_connection_lost)selfrs r__init__zFlowControlMixin.__init__sN  /1===DJJDJ )/11 %rctd|_|jrtjd|dSdS)NTz%r pauses writing)r?r> get_debugrdebugrDs r pause_writingzFlowControlMixin.pause_writingsB :   ! ! 4 L,d 3 3 3 3 3 4 4rcd|_|jrtjd||jD]+}|s|d,dS)NFz%r resumes writing)r?r>rGrrHrBdone set_resultrDwaiters rresume_writingzFlowControlMixin.resume_writingss :   ! ! 5 L-t 4 4 4) ( (F;;== (!!$''' ( (rcd|_|jsdS|jD]C}|s-||d.||DdSNT)rCr?rBrLrM set_exceptionrDexcrOs rconnection_lostz FlowControlMixin.connection_lostsy $|  F) . .F;;== ..%%d++++((---  . .rc2K|jrtd|jsdS|j}|j| |d{V|j|dS#|j|wxYw)NzConnection lost)rCConnectionResetErrorr?r> create_futurerBappendremoverNs r _drain_helperzFlowControlMixin._drain_helpers   :&'899 9|  F))++ ""6*** /LLLLLLL   & &v . . . . .D  & &v . . . .s A::Bctr)NotImplementedErrorrDstreams r_get_close_waiterz"FlowControlMixin._get_close_waiters!!rr) __name__ __module__ __qualname____doc__rErJrPrVr\rarrrr9r9us}&&&&444 ((( . . . / / /"""""rr9cleZdZdZdZd fd ZedZdZdZ fdZ dZ d Z d Z d ZxZS) ra=Helper class to adapt between Protocol and StreamReader. (This is a helper class instead of making StreamReader itself a Protocol subclass, because the StreamReader has other potential uses, and to prevent the user of the StreamReader to accidentally call inappropriate methods of the protocol.) NcLt||&tj||_|j|_nd|_|||_d|_d|_d|_ d|_ ||_ d|_ |j |_dS)NrF)superrEweakrefref_stream_reader_wr_source_traceback_strong_reader_reject_connection_stream_writer_task _transport_client_connected_cb _over_sslr>rY_closed)rD stream_readerr.r __class__s rrEzStreamReaderProtocol.__init__s d###  *%,[%?%?D "%2%DD " "%)D "  0#0D "'" $7!z//11 rc<|jdS|Sr)rkrIs r_stream_readerz#StreamReaderProtocol._stream_readers$  ! 4%%'''rcv|j}|j}||_||_|ddu|_dS)N sslcontext)r>r&rorqget_extra_infors)rDr(rr&s r_replace_writerz$StreamReaderProtocol._replace_writers>z$ $#"11,??tKrc|jrEddi}|jr |j|d<|j||dS||_|j}||||ddu|_ |j st||||j|_ | ||j }tj|r|j||_d|_dSdS)NmessagezpAn open stream was garbage collected prior to establishing network connection; call "stream.close()" explicitly.source_tracebackrz)rnrlr>call_exception_handlerabortrqrx set_transportr{rsrrrror iscoroutine create_taskrprm)rDr&contextr%ress rconnection_madez$StreamReaderProtocol.connection_mades5  " @G % E.2.D*+ J - -g 6 6 6 OO    F#$  ,   + + +"11,??tK  $ '".y$/5/3z#;#;D ++F,0,?AAC%c** 9!Z33C88 "&D    ' 'rc|j}|,||n|||js7||jdn|j|t |d|_d|_ d|_ d|_ dSr) rxfeed_eofrSrtrLrMrhrVrkrorprq)rDrUr%rvs rrVz$StreamReaderProtocol.connection_losts$  * *!!!!$$S)))|  "" 0 0 ''---- **3/// $$$!%" rcF|j}|||dSdSr)rx feed_data)rDdatar%s r data_receivedz"StreamReaderProtocol.data_receiveds6$  #   T " " " " " # #rcR|j}|||jrdSdS)NFT)rxrrs)rDr%s r eof_receivedz!StreamReaderProtocol.eof_receiveds8$   OO    > 5trc|jSr)rtr_s rraz&StreamReaderProtocol._get_close_waiters |rc |j}|r*|s|dSdSdS#t$rYdSwxYwr)rtrL cancelled exceptionAttributeError)rDcloseds r__del__zStreamReaderProtocol.__del__!s #\F{{}} #V%5%5%7%7 #  """"" # # # #    DD sA AANN)rbrcrdrerlrEpropertyrxr|rrVrrrar __classcell__)rvs@rrrs222222(((X( LLL'''6$###    # # # # # # #rrczeZdZdZdZdZedZdZdZ dZ dZ d Z d Z d Zdd ZdZd d ddZd S)ra'Wraps a Transport. This exposes write(), writelines(), [can_]write_eof(), get_extra_info() and close(). It adds drain() which returns an optional Future on which you can wait for flow control. It also adds a transport property which references the Transport directly. c||_||_||_||_|j|_|jddSr)rq _protocol_readerr>rY _complete_futrM)rDr&rr%rs rrEzStreamWriter.__init__7sS#!  !Z5577 %%d+++++rc|jjd|jg}|j|d|jdd|S)N transport=zreader=<{}> )rvrbrqrrZformatjoinrDinfos r__repr__zStreamWriter.__repr__As_')Ido)I)IJ < 4 KK2$,22 3 3 3}}SXXd^^,,,rc|jSrrqrIs rr&zStreamWriter.transportGs rc:|j|dSr)rqwriterDrs rrzStreamWriter.writeKs d#####rc:|j|dSr)rq writelinesrs rrzStreamWriter.writelinesNs ""4(((((rc4|jSr)rq write_eofrIs rrzStreamWriter.write_eofQs((***rc4|jSr)rq can_write_eofrIs rrzStreamWriter.can_write_eofTs,,...rc4|jSr)rqcloserIs rrzStreamWriter.closeWs$$&&&rc4|jSr)rq is_closingrIs rrzStreamWriter.is_closingZs))+++rcJK|j|d{VdSr)rrarIs r wait_closedzStreamWriter.wait_closed]s4n..t44444444444rNc8|j||Sr)rqr{)rDnamedefaults rr{zStreamWriter.get_extra_info`s--dG<< start_tlsrqr|)rDrzrrrr new_transports rrzStreamWriter.start_tls|sn9E >jjll"j22 OXz#_"7399999999 (  &&&&&rr)rbrcrdrerErrr&rrrrrrrr{rrrrrrr-s,,,--- X$$$)))+++///''',,,555====---4)-.2 ' ' ' ' ' ' 'rrceZdZdZedfdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZddZddZdZdZdZdS)rNcz|dkrtd||_|tj|_n||_t |_d|_d|_d|_ d|_ d|_ |j r-tjtjd|_dSdS)NrzLimit cannot be <= 0Fr ) ValueError_limitr r=r> bytearray_buffer_eof_waiter _exceptionrqr?rGr extract_stacksys _getframerl)rDrrs rrEzStreamReader.__init__s A: 5344 4  /11DJJDJ {{    :   ! ! "%3%A a  &"&"D " " " " "rc\dg}|jr*|t|jd|jr|d|jt kr|d|j|jr|d|j|jr|d|j|jr|d|j|j r|dd d |S) Nrz byteseofzlimit=zwaiter=z exception=rpausedrr) rrZlenrr_DEFAULT_LIMITrrrqr?rrrs rrzStreamReader.__repr__s. < 6 KK3t|,,444 5 5 5 9  KK    ;. ( 0 KK... / / / < 4 KK2$,22 3 3 3 ? : KK8T_88 9 9 9 ? : KK8T_88 9 9 9 < " KK ! ! !}}SXXd^^,,,rc|jSr)rrIs rrzStreamReader.exceptions rc||_|j}|2d|_|s||dSdSdSr)rrrrSrTs rrSzStreamReader.set_exceptionsa  *DL##%% *$$S))))) * * * *rc|j}|2d|_|s|ddSdSdS)z1Wakeup read*() functions waiting for data or EOF.N)rrrMrNs r_wakeup_waiterzStreamReader._wakeup_waitersZ  (DL##%% (!!$''''' ( ( ( (rc||_dSrr)rDr&s rrzStreamReader.set_transports #rc|jr?t|j|jkr$d|_|jdSdSdS)NF)r?rrrrqresume_readingrIs r_maybe_resume_transportz$StreamReader._maybe_resume_transportsY < -C --< - DL O * * , , , , , - - - -rc<d|_|dSrR)rrrIs rrzStreamReader.feed_eofs!  rc |jo|j S)z=Return True if the buffer is empty and 'feed_eof' was called.)rrrIs rat_eofzStreamReader.at_eofsy---rcD|sdS|j|||jb|js]t |jd|jzkr? |jd|_dS#t$r d|_YdSwxYwdSdSdS)NT) rextendrrqr?rr pause_readingr^rs rrzStreamReader.feed_datas  F D!!!  O $L $DL!!A O3 $ $--/// $ ' ' ' '#'  ' $ $ $ $ $ $s"BBBcK|jt|d|jr d|_|j|j|_ |jd{Vd|_dS#d|_wxYw)zpWait until feed_data() or feed_eof() is called. If stream was paused, automatically resume it. NzF() called while another coroutine is already waiting for incoming dataF)r RuntimeErrorr?rqrr>rY)rD func_names r_wait_for_datazStreamReader._wait_for_datas < 655566 6 < - DL O * * , , ,z//11  ,       DLLL4DL    s " A88 BcKd}t|} ||d{V}n#tj$r}|jcYd}~Sd}~wtj$r}|j||jr|jd|j|z=n|j | t|j dd}~wwxYw|S)aRead chunk of data from the stream until newline (b' ') is found. On success, return chunk that ends with newline. If only partial line can be read due to EOF, return incomplete line without terminating newline. When EOF was reached while no bytes read, empty bytes object is returned. If limit is reached, ValueError will be raised. In that case, if newline was found, complete line including newline will be removed from internal buffer. Else, internal buffer will be cleared. Limit is compared against part of the line without newline. If stream was paused, this function will automatically resume it if needed.  Nr) r readuntilrIncompleteReadErrorpartialLimitOverrunErrorr startswithconsumedclearrrargs)rDsepseplenlinees rreadlinezStreamReader.readlines  S (,,,,,,,,DD-   9      + ( ( (|&&sAJ77 %L!5!*v"5!566 ""$$$  ( ( * * *QVAY'' '  ( s(1CA C CA:CCrcKt|}|dkrtd|j|jd} t|j}||z |krJ|j||}|dkrn|dz|z }||jkrt jd||jrBt|j}|j t j |d| dd{V||jkrt jd ||jd||z}|jd||z=| t|S) aVRead data from the stream until ``separator`` is found. On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end. Configured stream limit is used to check result. Limit sets the maximal length of data that can be returned, not counting the separator. If an EOF occurs and the complete separator is still not found, an IncompleteReadError exception will be raised, and the internal buffer will be reset. The IncompleteReadError.partial attribute may contain the separator partially. If the data cannot be read because of over limit, a LimitOverrunError exception will be raised, and the data will be left in the internal buffer, so it can be read again. rz,Separator should be at least one-byte stringNTr z2Separator is not found, and chunk exceed the limitrz2Separator is found, but chunk is longer than limit)rrrrfindrrrrbytesrrrr)rD separatorroffsetbuflenisepchunks rrzStreamReader.readuntil-s(Y Q; MKLL L ? "/ !* 3&&F&( |((F;;2: !f,DK' $6L   y Bdl++ ""$$$ 4UDAAA%%k22 2 2 2 2 2 2 2= 3@ $+  L.DdLL L ^dVm^, L$- ( $$&&&U||rrcK|j|j|dkrdS|dkrQg} ||jd{V}|sn||9d|S|js"|js|dd{Vt|jd|}|jd|=| |S)aRead up to `n` bytes from the stream. If n is not provided, or set to -1, read until EOF and return all read bytes. If the EOF was received and the internal buffer is empty, return an empty bytes object. If n is zero, return empty bytes object immediately. If n is positive, this function try to read `n` bytes, and may return less or equal bytes than requested, but at least one byte. If EOF was received before any byte is read, this function returns empty byte object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. NrrTread) rrrrZrrrrrr)rDnblocksblockrs rrzStreamReader.reads* ? "/ ! 6 3 q5 $ F %"ii 44444444 e$$$  % 88F## #| .DI .%%f-- - - - - - - -T\"1"%&& L!  $$&&& rcK|dkrtd|j|j|dkrdSt|j|kr||jrBt |j}|jtj||| dd{Vt|j|k|t|j|kr.t |j}|jn&t |jd|}|jd|=| |S)aRead exactly `n` bytes. Raise an IncompleteReadError if EOF is reached before `n` bytes can be read. The IncompleteReadError.partial attribute of the exception will contain the partial read bytes. if n is zero, return empty bytes object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. rz*readexactly size can not be less than zeroNr readexactly) rrrrrrrrrrr)rDr incompleters rrzStreamReader.readexactlysZ q5 KIJJ J ? "/ ! 6 3$,!# 5y D"4<00  ""$$$ 4ZCCC%%m44 4 4 4 4 4 4 4 $,!# 5 t|   ! !&&D L   bqb)**D RaR  $$&&& rc|SrrrIs r __aiter__zStreamReader.__aiter__s rcXK|d{V}|dkrt|S)Nr)rStopAsyncIteration)rDvals r __anext__zStreamReader.__anext__s<MMOO###### #: %$ $ r)r)r)rbrcrdrlrrErrrSrrrrrrrrrrrr r rrrrrs4+$"""",---$***((($$$--- ...$$$,   8>YYYYv0000d'''Rrrrr)__all__r@socketrwarningsrihasattrr r rrrlogrtasksrrrrr r Protocolr9rrrrrrrs^ '  769= <