)L i4fdZddlmZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z ddl mZmZddlmZddlmZdd lmZmZdd lmZmZdd lmZdd lmZdd lmZm Z ddl!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0dgZ1ddZ2ddZ3GddZ4GddZ5ddZ6GddZ7y)z Telnet server. ) annotationsN)get_running_loop)AnyCallable CoroutineTextIOcast)create_app_sessionget_app)run_in_terminal)Size)AnyFormattedTextto_formatted_text) PipeInputcreate_pipe_input) Vt100_Output)print_formatted_text) BaseStyle DummyStyle)logger) DOECHOIACLINEMODEMODENAWSSBSESENDSUPPRESS_GO_AHEADTTYPEWILLTelnetProtocolParser TelnetServerct|fSN)bytes)numbers j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/contrib/telnet/server.pyint2byter+,s & ctjd|jttzt z|jtt ztz|jttzt ztztdztztz|jtt ztz|jttztz|jttztz|jttztztztztzy)NzInitializing telnet connectionr)rinfosendrrrr#r!rrr+rrrr"r  connections r*_initialize_telnetr20s KK01OOC"Hx'(OOC$J!223OOC"Hx'$.!rHrMrErTpropertyr=rArKr,r*r4r4MsH  r,r4cveZdZdZ d d dZd dZddZd dZddZddZ ddZ d d Z y )TelnetConnectionz6 Class that represents one Telnet connection. c  |_|_|_|_|_|_d_tj_ |_ _ d_ tdd_t|d fd t!t"t%||_d fd } d fd } d  fd } t)| | | _d_y)NF(OrowscolumnscjSr')sizerLsr*get_sizez+TelnetConnection.__init__..get_sizes 99 r,)r=c<jj|y)z-TelnetProtocolParser 'data_received' callbackN) vt100_input send_bytesrGr<s r* data_receivedz0TelnetConnection.__init__..data_receiveds    ' ' -r,ct||_j*jrjj dyyy)z-TelnetProtocolParser 'size_received' callbackrgNc2tjSr')r _on_resizerKr,r*zBTelnetConnection.__init__..size_received..s)=)=)?r,)r rk vt100_outputcontextrun)rhrir<s r* size_receivedz0TelnetConnection.__init__..size_receiveds>$8DI  ,   !?@2>,r,cvtj|_jj y)z.TelnetProtocolParser 'ttype_received' callback)term enable_cprN)rstdoutrv_readyset)ttyper|rlr<s r*ttype_receivedz1TelnetConnection.__init__..ttype_receiveds- , XEj!D  KKOO r,)rYr rGr(rYrZ)rhintrirrYrZ)rrXrYrZ)connaddrinteractserverr=styler;asyncioEventr~rnr|rvr rkr2r rr4r}r$parserrw) r<rrrrr=rrnr|rqryrrls ` ` @r*r>zTelnetConnection.__init__|s         mmo &$15b"-  4  6#4TH#MN  . A  +=-X 37 r,cKdfd }t}|jj| jj d{t j j5tj_ jd{dddjy7q7#1swYxYw#jwxYww)z" Run application. cjjd}|rj|ytjdj j jy)Niz&Connection closed by client. {!r} {!r})rrecvfeedrr.formatrrTrps r*handle_incoming_dataz>TelnetConnection.run_application..handle_incoming_datasM99>>$'D $ KDKKTYYWX r,N)inputoutputr\) r add_readerrr~waitr rnrv contextvars copy_contextrwrrT)r<rloops` r*run_applicationz TelnetConnection.run_applications   !  #78 ++""$ $ $#$*:*:4CTCTU **779 mmD))) * JJL %* * * JJLsX-C%CC%C4.C"C#C'C/C%CCC  CC""C%c:|jj|y)zF Handler for incoming data. (Called by TelnetServer.) N)rrrFs r*rzTelnetConnection.feeds r,c|jsyd|_|jjtj |j |j j|j jyy)z# Closed by client. TN)r;rnrTr remove_readerrr}rLs r*rTzTelnetConnection.closes\||DL    " " $   , ,TYY 7 IIOO  KK    r,c|jyt|}t|j||jxs t y)z* Send text to the client. N)rvrrrrr<formatted_texts r*r/zTelnetConnection.sends=    $ *>:   ~tzz/IZ\ r,cFtjfdS)z\ Send text to the client. This is asynchronous, returns a `Future`. c&jSr')r/)rr<sr*ruz4TelnetConnection.send_above_prompt..sTYY~-Fr,)r_run_in_terminalrs``r*send_above_promptz"TelnetConnection.send_above_prompts! +>:$$%FGGr,cr|jr!|jjt|ytd)Nz2Called _run_in_terminal outside `run_application`.)rwrxr RuntimeError)r<funcs r*rz!TelnetConnection._run_in_terminals+ << LL  _d 3ST Tr,c|jy|jj|jjdd|jjy)zB Erase the screen and move the cursor to the top. Nr)rv erase_screen cursor_gotorErLs r*rzTelnetConnection.erase_screensM    $  &&( %%a+ !r,N)T)rrWrztuple[str, int]r7Callable[[TelnetConnection], Coroutine[Any, Any, None]]rr%r=rXrBaseStyle | Nonernrr|r[rYrZr\r)rrrYrZ)rzCallable[[], None]rYrZ) r]r^r_r`r>rrrTr/rrrrKr,r*rcrcws 585858J 58  58  58 585858 58n6    HU"r,rcc Kywr'rKr0s r*_dummy_interactrs scteZdZdZddedddf d dZeddZddd Zdd Z dd Z dd Z y)r%a Telnet server implementation. Example:: async def interact(connection): connection.send("Welcome") session = PromptSession() result = await session.prompt_async(message="Say something: ") connection.send(f"You said: {result} ") async def main(): server = TelnetServer(interact=interact, port=2323) await server.run() z 127.0.0.1zutf-8NTc||_||_||_||_||_||_d|_g|_t|_ yr') hostportrr=rr| _run_task_application_tasksr connections)r<rrrr=rr|s r*r>zTelnetServer.__init__sG       $48<>25%r,ctjtjtj}|jtjtj d|j ||f|jd|S)Nr)socketAF_INET SOCK_STREAM setsockopt SOL_SOCKET SO_REUSEADDRbindlisten)clsrrss r*_create_socketzTelnetServer._create_socket2sZ MM&..&*<*< = V&&(;(;Q? d|  r,cKjjjtjdjjt j fd|r| tjd{t jjjD]}|jtjdkDr9tjjdtjd{yy77#t jjjD]}|jtjdkDr:tjjdtjd{7wwxYww)z Run the telnet server, until this gets cancelled. :param ready_cb: Callback that will be called at the point that we're actually listening. z.Listening for telnet connections on %s port %rc&jSr')_accept)r<rsr*ruz"TelnetServer.run..Hsdll66Jr,Nr)timeout return_when)rrrrr.rrrFuturerrTrcancellenr ALL_COMPLETED)r<ready_cbtrs` @r*rxzTelnetServer.run<s$$TYY : D;D7D;BG0D91G7D;9G;BGGGGcv|jytj|j|_y)z} Deprecated: Use `.run()` instead. Start the telnet server (stop by calling and awaiting `stop()`). N)rr create_taskrxrLs r*startzTelnetServer.startcs, >> % )+77 Cr,cK|j0|jj |jd{yy7#tj$rYywxYww)z Deprecated: Use `.run()` instead. Stop a telnet server that was started using `.start()` and wait for the cancellation to complete. N)rrrCancelledErrorrLs r*stopzTelnetServer.stoposU >> % NN ! ! # nn$$ &%))  s7'AAAAAAAAAAc|j\tjdgdfd }tj |j j y)z1 Accept new incoming connection. zNew connection %r %rc K t5}tjjj|j }j j|tjdg |jd{j j|tjdg dddj(jy7Z#j j|tjdgwxYw#1swY]xYw#t$rtjdYt$rtjdYt$r@}tdt!|j"|ddl}|j'Yd}~d}~wwxYw#j(jwxYww)N)r=rrnr|zStarting interaction %r %rzStopping interaction %r %rz)Unhandled EOFError in telnet application.z2Unhandled KeyboardInterrupt in telnet application.zGot r)rrcrr=rr|raddrr.rremoveEOFErrorKeyboardInterrupt BaseExceptionprinttyper] traceback print_excr)rnr1rRrrrr<tasks r*rxz!TelnetServer._accept..runs# 5&(IK!1 !%"jj$/#'?? "J$$((4KK <DtDI(88:::((// ; $@H4H%ID''..t4';((// ; $@H4H%II& I  GH$ R PQ  &T!W--./3 ##%%  & ''..t4sG D/A-D#=C-C+C-1D#D/G+C--3D  D##D,(D//F6 F9F6,F9.F666F1,F91F66F99GGNr\)acceptrr.rrrrC)r< listen_socketrxrrrs` @@@r*rzTelnetServer._accept}s_#))+ d *2T2$ 5$ 5L !--ce4 &&t,r,)rrXrrrrr=rXrrr|r[rYrZ)rrXrrrYrWr')rzCallable[[], None] | NonerYrZr\)rrWrYrZ) r]r^r_r`rr> classmethodrrxrrrrKr,r*r%r% s$  "&888 88 88 8.%N D /-r,)r)rrYr()r1rWrYrZ)r1rcrYrZ)8r` __future__rrrrrtypingrrrrr "prompt_toolkit.application.currentr r *prompt_toolkit.application.run_in_terminalr prompt_toolkit.data_structuresr prompt_toolkit.formatted_textrrprompt_toolkit.inputrrprompt_toolkit.output.vt100rprompt_toolkit.rendererrprompt_toolkit.stylesrrlogrprotocolrrrrrrrrr r!r"r#r$__all__r+r2r4rcrr%rKr,r*rs# $99JF/M=4P7"  8:''TL"L"^ b-b-r,