
    h.                        S r SSKrSSKJr  SSKJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr   " S	 S
5      r\" \
5       " S S5      5       r\" \R&                  \R(                  5       " S S5      5       rS rS r\4S jrS rS r\" \R&                  \R(                  5       " S S5      5       r " S S\R8                  5      r " S S\R<                  5      rSS jr SS jr!g)zF
Testing support for protocols -- loopback between client and server.
    N)implementer)defer
interfacesmainprotocol)IAddress)
deferLater)policies)failurec                   <    \ rS rSrSrSrSrS rS rS r	\	r
S rS	rg)
_LoopbackQueue   z
Trivial wrapper around a list to give it an interface like a queue, which
the addition of also sending notifications by way of a Deferred whenever
the list has something added to it.
NFc                     / U l         g N)_queueselfs    a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/protocols/loopback.py__init___LoopbackQueue.__init__"   s	        c                     U R                   R                  U5        U R                  b%  U R                  S so l        UR                  S 5        g g r   )r   append_notificationDeferredcallback)r   vds      r   put_LoopbackQueue.put%   sD    1%%1,0,F,F)A)JJt 2r   c                 ,    [        U R                  5      $ r   )boolr   r   s    r   __nonzero___LoopbackQueue.__nonzero__+   s    DKK  r   c                 8    U R                   R                  S5      $ Nr   )r   popr   s    r   get_LoopbackQueue.get0   s    {{q!!r   )r   r   )__name__
__module____qualname____firstlineno____doc__r   
disconnectr   r   r"   __bool__r'   __static_attributes__ r   r   r   r      s/     !J! H"r   r   c                       \ rS rSrSrg)_LoopbackAddress4   r1   N)r)   r*   r+   r,   r0   r1   r   r   r3   r3   4   s    r   r3   c                   X    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
 rS rS rS rSrg)_LoopbackTransport9   FNc                     Xl         g r   )q)r   r9   s     r   r   _LoopbackTransport.__init__?   s    r   c                 z    [        U[        5      (       d  [        S5      eU R                  R	                  U5        g )Nz"Can only write bytes to ITransport)
isinstancebytes	TypeErrorr9   r   r   datas     r   write_LoopbackTransport.writeB   s+    $&&@AA

4r   c                 X    U R                   R                  SR                  U5      5        g Nr   )r9   r   joinr   iovecs     r   writeSequence _LoopbackTransport.writeSequenceG   s    

388E?#r   c                 \    SU R                   l        U R                   R                  S 5        g )NT)r9   r.   r   r   s    r   loseConnection!_LoopbackTransport.loseConnectionJ   s     

4r   c                 $    U R                  5         g)z2
Abort the connection. Same as L{loseConnection}.
N)rK   r   s    r   abortConnection"_LoopbackTransport.abortConnectionN   s     	r   c                     [        5       $ r   r3   r   s    r   getPeer_LoopbackTransport.getPeerT       !!r   c                     [        5       $ r   rQ   r   s    r   getHost_LoopbackTransport.getHostW   rT   r   c                 Z    U R                   b   eXl         X l        U R                  5         g r   )producerstreamingProducer_pollProducerr   rY   	streamings      r   registerProducer#_LoopbackTransport.registerProducer[   s)    }}$$$ !*r   c                 0    U R                   c   eS U l         g r   rY   r   s    r   unregisterProducer%_LoopbackTransport.unregisterProducera   s    }}(((r   c                 x    U R                   b-  U R                  (       d  U R                   R                  5         g g g r   )rY   rZ   resumeProducingr   s    r   r[    _LoopbackTransport._pollProducere   s-    ==$T-C-CMM))+ .D$r   )rY   r9   rZ   )r)   r*   r+   r,   disconnectingrY   r   rA   rH   rK   rN   rR   rV   r^   rb   r[   r0   r1   r   r   r6   r6   9   s>    MH
$"",r   r6   c                 p    U (       a/  U R                  5       nUc  gUR                  U5        U (       a  M.  gg)z
L{identityPumpPolicy} is a policy which delivers each chunk of data written
to the given queue as-is to the target.

This isn't a particularly realistic policy.

@see: L{loopbackAsync}
N)r'   dataReceived)queuetargetr=   s      r   identityPumpPolicyrl   j   s/     		=E"	 %r   c                     / nU (       a.  U R                  5       nUc  OUR                  U5        U (       a  M.  U(       a!  UR                  SR                  U5      5        gg)z
L{collapsingPumpPolicy} is a policy which collapses all outstanding chunks
into a single string and delivers it to the target.

@see: L{loopbackAsync}
Nr   )r'   r   ri   rE   )rj   rk   r=   chunks       r   collapsingPumpPolicyro   z   sR     E
		=U	 %
 CHHUO, r   c                     [        5       n[        5       nU R                  [        U5      5        UR                  [        U5      5        [        XXU5      $ )a  
Establish a connection between C{server} and C{client} then transfer data
between them until the connection is closed. This is often useful for
testing a protocol.

@param server: The protocol instance representing the server-side of this
    connection.

@param client: The protocol instance representing the client-side of this
    connection.

@param pumpPolicy: When either C{server} or C{client} writes to its
    transport, the string passed in is added to a queue of data for the
    other protocol.  Eventually, C{pumpPolicy} will be called with one such
    queue and the corresponding protocol object.  The pump policy callable
    is responsible for emptying the queue and passing the strings it
    contains to the given protocol's C{dataReceived} method.  The signature
    of C{pumpPolicy} is C{(queue, protocol)}.  C{queue} is an object with a
    C{get} method which will return the next string written to the
    transport, or L{None} if the transport has been disconnected, and which
    evaluates to C{True} if and only if there are more items to be
    retrieved via C{get}.

@return: A L{Deferred} which fires when the connection has been closed and
    both sides have received notification of this.
)r   makeConnectionr6   _loopbackAsyncBody)serverclient
pumpPolicyserverToClientclientToServers        r   loopbackAsyncrx      sP    6 $%N#%N
,^<=
,^<=
 r   c                 T  ^ U4S jn S=n=pxU" XU5      nU" X#U 5      nU(       dD  U(       d=  [         R                  " 5       n	Xl        Xl        U	R                  [        U UUUT5        U	$ UR
                  (       a  SnU" XU5        OUR
                  (       a  SnU" X#U 5        U(       a|  U R                  [        R                  " [        R                  5      5        UR                  [        R                  " [        R                  5      5        [         R                  " S5      $ GM!  )a?  
Transfer bytes from the output queue of each protocol to the input of the other.

@param server: The protocol instance representing the server-side of this
connection.

@param serverToClient: The L{_LoopbackQueue} holding the server's output.

@param client: The protocol instance representing the client-side of this
connection.

@param clientToServer: The L{_LoopbackQueue} holding the client's output.

@param pumpPolicy: See L{loopbackAsync}.

@return: A L{Deferred} which fires when the connection has been closed and
    both sides have received notification of this.
c                 ~   > SnU(       a
  T" X5        SnU(       a!  U(       d  U R                   R                  5         U$ )NFT)	transportr[   )sourcer9   rk   sentru   s       r   pump _loopbackAsyncBody.<locals>.pump   s6    q!D **,r   FTN)r   Deferredr   addCallback_loopbackAsyncContinuer.   connectionLostr   Failurer   CONNECTION_DONEsucceed)
rs   rv   rt   rw   ru   r~   r.   
clientSent
serverSentr   s
       `     r   rr   rr      s    (
 /44
4Z &&9
&&9
*  A340340MM& H$$ J0&& J0!!'//$2F2F"GH!!'//$2F2F"GH==&&I r   c           
      V    S Ul         S Ul         SSKJn  [        US[        UUUUU5      $ )Nr   reactor)r   twisted.internetr   r	   rr   )ignoredrs   rv   rt   rw   ru   r   s          r   r   r      s>    
 ,0N(+/N( )		 	r   c                   d    \ rS rSrSrSrSrSrSS jrS r	S r
S rS	 rS
 rS rS rS rS rSrg)LoopbackRelayi  r   r   Nc                     Xl         X l        g r   )rk   logFile)r   rk   r   s      r   r   LoopbackRelay.__init__  s    r   c                     U R                   U-   U l         U R                  (       a(  U R                  R                  S[        U5      -  5        g g )Nzloopback writing %s
)bufferr   rA   reprr?   s     r   rA   LoopbackRelay.write  s:    kkD(<<LL6dCD r   c                 D    U R                  SR                  U5      5        g rD   )rA   rE   rF   s     r   rH   LoopbackRelay.writeSequence"  s    

388E?#r   c                 (   U R                   S:X  a  g U R                  (       a  U R                  R                  5         U R                  (       ap  U R                  (       a1  U R                  R                  S[        U R                  5      -  5        U R                  nSU l        U R                  R                  U5        U R                   S:X  aE  SU l         U R                  R                  [        R                  " [        R                  5      5        g g )Nzloopback receiving %s
r      )
shouldLoserY   re   r   r   rA   r   rk   ri   r   r   r   r   r   )r   r   s     r   clearBufferLoopbackRelay.clearBuffer%  s    ??b ==MM))+;;||""#<tDKK?P#PQ[[FDKKK$$V,??a DOKK&&wt7K7K'LM  r   c                 4    U R                   S:w  a  SU l         g g )Nr   r   )r   r   s    r   rK   LoopbackRelay.loseConnection5  s    ??b DO !r   c                     gNloopbackr1   r   s    r   rV   LoopbackRelay.getHost9      r   c                     gr   r1   r   s    r   rR   LoopbackRelay.getPeer<  r   r   c                     Xl         g r   ra   r\   s      r   r^   LoopbackRelay.registerProducer?  s     r   c                     S U l         g r   ra   r   s    r   rb    LoopbackRelay.unregisterProducerB  s	    r   c                 L    SU R                   R                  R                  < S3$ )Nz	Loopback())rk   	__class__r)   r   s    r   	logPrefixLoopbackRelay.logPrefixE  s"    4;;0099<A>>r   )r   r   rY   r   rk   r   )r)   r*   r+   r,   r   r   rg   rY   r   rA   rH   r   rK   rV   rR   r^   rb   r   r0   r1   r   r   r   r     sJ    FJMHE
$N  !?r   r   c                   &    \ rS rSrS rS rS rSrg)LoopbackClientFactoryiI  c                 R    SU l         [        R                  " 5       U l        Xl        g r%   )disconnectedr   r   deferredr   )r   r   s     r   r   LoopbackClientFactory.__init__J  s    ( r   c                     U R                   $ r   )r   )r   addrs     r   buildProtocol#LoopbackClientFactory.buildProtocolO  s    }}r   c                 H    SU l         U R                  R                  S 5        g )Nr   )r   r   r   )r   	connectorreasons      r   clientConnectionLost*LoopbackClientFactory.clientConnectionLostR  s    t$r   )r   r   r   N)r)   r*   r+   r,   r   r   r   r0   r1   r   r   r   r   I  s    !
%r   r   c                        \ rS rSrS rS rSrg)_FireOnCloseiW  c                 x    [         R                  R                  XU5        [        R                  " 5       U l        g r   )r
   ProtocolWrapperr   r   r   r   )r   r   factorys      r   r   _FireOnClose.__init__X  s&      ))$'B(r   c                 x    [         R                  R                  X5        U R                  R	                  S 5        g r   )r
   r   r   r   r   )r   r   s     r   r   _FireOnClose.connectionLost\  s(      //=t$r   r   N)r)   r*   r+   r,   r   r   r0   r1   r   r   r   r   W  s    )%r   r   c                   ^^	 SSK Jn  [        R                  " [        R
                  " 5       5      n[        XP5      m	X5l        U	4S jUl        UR                  X%SS9m[        U5      nX6l        UR                  STR                  5       R                  U5        UR                  nUR                  U	4S j5        UR                  U4S j5        U$ )zBRun session between server and client protocol instances over TCP.r   r   c                    > T$ r   r1   r   serverWrappers    r   <lambda>loopbackTCP.<locals>.<lambda>h      =r   z	127.0.0.1)	interfacec                    > TR                   $ r   r   xr   s    r   r   r   n      M22r   c                 $   > TR                  5       $ r   stopListeningr   
serverPorts    r   r   r   o      J446r   )r   r   r
   WrappingFactoryr   Factoryr   noisyr   	listenTCPr   
connectTCPrV   portr   r   )
rs   rt   r   r   r   fclientFr   r   r   s
           @@r   loopbackTCPr   a  s    (  !1!1!34A +MG0AO""4k"BJ#F+GM{J$6$6$8$=$=wGAMM23MM67Hr   c                   ^^	 [         R                  " 5       nSSKJn  [        R
                  " [        R                  " 5       5      n[        XP5      m	X%l	        U	4S jUl
        UR                  X55      m[        U5      nX&l	        UR                  X65        UR                  nUR                  U	4S j5        UR                  U4S j5        U$ )zJRun session between server and client protocol instances over UNIX socket.r   r   c                    > T$ r   r1   r   s    r   r   loopbackUNIX.<locals>.<lambda>{  r   r   c                    > TR                   $ r   r   r   s    r   r   r     r   r   c                 $   > TR                  5       $ r   r   r   s    r   r   r     r   r   )tempfilemktempr   r   r
   r   r   r   r   r   r   
listenUNIXr   connectUNIXr   r   )
rs   rt   r   pathr   r   r   r   r   r   s
           @@r   loopbackUNIXr   s  s    ??D(  !1!1!34A +MG0AO##D,J#F+GM&AMM23MM67Hr   )r   T)T)"r-   r   zope.interfacer   r   r   r   r   r   twisted.internet.interfacesr   twisted.internet.taskr	   twisted.protocolsr
   twisted.pythonr   r   r3   
ITransport	IConsumerr6   rl   ro   rx   rr   r   r   ClientFactoryr   r   r   r   r   r1   r   r   <module>r      s   
  & > > 0 , ' "" "8 X	 	 	 Z""J$8$89-, -, :-,`# -" .@ #LD'N4 Z""J$8$893? 3? :3?l%H22 %%8++ %$r   