
    h
J                       S r SSKJr  SSKr SSKJr  SSKJ	r	J
r
  SSKJrJr  SSKJrJr  SSKJr  SS	KJrJr  SS
KJrJr  SSKJr   " S S5      r\
" \R8                  5       " S S5      5       r\
" \R<                  \R>                  \R@                  5       " S S5      5       r!S r"S r# " S S5      r$   SS jr%\"\#SSS4S jr&S r' " S S5      r(SS jr)g! \ a     Nf = f) z0
Utilities and helpers for simulating a network
    )annotationsN)Error)directlyProvidesimplementer)error
interfaces)TCP4ClientEndpointTCP4ServerEndpoint)ConnectionRefusedError)FactoryProtocol)MemoryReactorMemoryReactorClock)Failurec                  *    \ rS rSrS rSS jrS rSrg)TLSNegotiation   c                6    Xl         X l        SU l        X l        g )NF)objconnectStatesentreadyToSend)selfr   r   s      Y/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/iosim.py__init__TLSNegotiation.__init__   s    (	'    c                $    SU R                   < S3$ )NzTLSNegotiation())r   r   s    r   __repr__TLSNegotiation.__repr__"   s     A..r   c                    U R                   R                  UR                   5      (       d   [        5       Ul        UR	                  5         g g N)r   iosimVerifyNativeOpenSSLErrordisconnectReasonloseConnection)r   othertpts      r   pretendToVerifyTLSNegotiation.pretendToVerify%   s9     xx##EII..#5#7C   /r   )r   r   r   r   Nreturnstr)__name__
__module____qualname____firstlineno__r   r!   r+   __static_attributes__ r   r   r   r      s    (/!r   r   c                      \ rS rSrSrSrg)FakeAddress.   zQ
The default address type for the host and peer of L{FakeTransport}
connections.
r5   N)r0   r1   r2   r3   __doc__r4   r5   r   r   r7   r7   .   s    r   r7   c                  ,   \ rS rSrSr\" \R                  " 5       4S j5      rSr	Sr
Sr\R                  " S5      rSrSrSrS!S j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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+g)$FakeTransport6   z
A wrapper around a file-like object to make it behave as a Transport.

This doesn't actually stream the file to the attached protocol,
and is thus useful mainly as a utility for debugging protocols.
c                *    [        [        U 5      5      $ r$   )intnext)counters    r   <lambda>FakeTransport.<lambda>?   s    T']ASr   r   zConnection doneNc                    Xl         X l        / U l        U R                  5       U l        Uc
  [        5       nX0l        Uc
  [        5       nX@l        g)at  
@param protocol: This transport will deliver bytes to this protocol.
@type protocol: L{IProtocol} provider

@param isServer: C{True} if this is the accepting side of the
    connection, C{False} if it is the connecting side.
@type isServer: L{bool}

@param hostAddress: The value to return from C{getHost}.  L{None}
    results in a new L{FakeAddress} being created to use as the value.
@type hostAddress: L{IAddress} provider or L{None}

@param peerAddress: The value to return from C{getPeer}.  L{None}
    results in a new L{FakeAddress} being created to use as the value.
@type peerAddress: L{IAddress} provider or L{None}
N)protocolisServerstream_nextserialserialr7   hostAddresspeerAddress)r   rD   rE   rI   rJ   s        r   r   FakeTransport.__init__H   sK    " ! &&(%-K&%-K&r   c                    SR                  U R                  =(       a    S=(       d    SU R                  U R                  R                  R
                  5      $ )NzFakeTransport<{},{},{}>SC)formatrE   rH   rD   	__class__r0   r    s    r   r!   FakeTransport.__repr__d   s@    (//MM!c(SKKMM##,,
 	
r   c                    U R                   (       a  g U R                  b  U R                  R                  U5        g U R                  R                  U5        g r$   )disconnectingtlstlsbufappendrF   )r   datas     r   writeFakeTransport.writek   s<    88KKt$KKt$r   c                    U R                   (       a-  U R                  (       d  U R                   R                  5         g g g r$   producerstreamingProducerresumeProducingr    s    r   _checkProducerFakeTransport._checkProduceru   s*     ==!7!7MM))+ "8=r   c                L    Xl         X l        U(       d  UR                  5         gg)z
From abstract.FileDescriptor
Nr[   )r   r\   	streamings      r   registerProducerFakeTransport.registerProducer{   s#     !!*$$& r   c                    S U l         g r$   )r\   r    s    r   unregisterProducer FakeTransport.unregisterProducer   s	    r   c                D    U R                  5         U R                  5         g r$   )rf   r(   r    s    r   stopConsumingFakeTransport.stopConsuming   s    !r   c                D    U R                  SR                  U5      5        g )Nr   )rX   join)r   iovecs     r   writeSequenceFakeTransport.writeSequence   s    

388E?#r   c                    SU l         g NTrS   r    s    r   r(   FakeTransport.loseConnection   s
    !r   c                    SU l         g)zX
For the time being, this is the same as loseConnection; no buffered
data will be lost.
TNrr   r    s    r   abortConnectionFakeTransport.abortConnection   s    
 "r   c                    U R                   b  [        5       nOU R                  nU R                  R	                  [        U5      5        g r$   )rT   r&   r'   rD   connectionLostr   )r   errs     r   reportDisconnectFakeTransport.reportDisconnect   s7    88 %&C''C$$WS\2r   c                    g)z=
Identify this transport/event source to the logging system.
iosimr5   r    s    r   	logPrefixFakeTransport.logPrefix   s     r   c                    U R                   $ r$   )rJ   r    s    r   getPeerFakeTransport.getPeer       r   c                    U R                   $ r$   )rI   r    s    r   getHostFakeTransport.getHost   r   r   c                    g r$   r5   r    s    r   r^   FakeTransport.resumeProducing       r   c                    g r$   r5   r    s    r   pauseProducingFakeTransport.pauseProducing   r   r   c                $    U R                  5         g r$   )r(   r    s    r   stopProducingFakeTransport.stopProducing   s    r   c                P    U R                   U-  n[        X5      U l        / U l        g r$   )rE   r   rT   rU   )r   contextFactorybeNormalr   s       r   startTLSFakeTransport.startTLS   s%     }}x/!.?r   c                    U R                   nU(       a  / U l         SR                  U5      $ U R                  b9  U R                  R                  (       a  SU R                  l        U R                  $ gg)z
Get the pending writes from this transport, clearing them from the
pending buffer.

@return: the bytes written with C{transport.write}
@rtype: L{bytes}
r   NT)rF   rl   rT   r   r   )r   rM   s     r   getOutBufferFakeTransport.getOutBuffer   sV     KKDK88A;XX!xx## $xxr   c                   [        U[        5      (       a  U R                  c   eU R                  R                  (       aa  U R                  R	                  X5        S U l        U R
                  S so l        U R                  U5        [        U [        R                  5        g SU R                  l
        g U R                  R                  U5        g rq   )
isinstancer   rT   r   r+   rU   rn   r   r   ISSLTransportr   rD   dataReceived)r   bufbs      r   bufferReceivedFakeTransport.bufferReceived   s    c>**88''' xx}}((3 "&d;""1% z'?'?@ (,$MM&&s+r   c                    g r$   r5   r    s    r   getTcpKeepAliveFakeTransport.getTcpKeepAlive   r   r   c                    g r$   r5   r    s    r   getTcpNoDelayFakeTransport.getTcpNoDelay   r   r   c                    g r$   r5   r    s    r   loseWriteConnection!FakeTransport.loseWriteConnection   r   r   c                    g r$   r5   r   enableds     r   setTcpKeepAliveFakeTransport.setTcpKeepAlive   r   r   c                    g r$   r5   r   s     r   setTcpNoDelayFakeTransport.setTcpNoDelay   r   r   )rS   rI   rE   rJ   r\   rD   rH   rF   r]   rT   rU   )NNr-   )T),r0   r1   r2   r3   r9   staticmethod	itertoolscountrG   closedrS   disconnectedr   ConnectionDoner'   r\   r]   rT   r   r!   rX   r_   rc   rf   ri   rn   r(   ru   rz   r~   r   r   r^   r   r   r   r   r   r   r   r   r   r   r4   r5   r   r   r;   r;   6   s     ioo.?STKFML++,=>H
C'8
%,'$""3  ,,&r   r;   c                    [        U SS9$ )z
Create and return a new in-memory transport hooked up to the given protocol.

@param clientProtocol: The client protocol to use.
@type clientProtocol: L{IProtocol} provider

@return: The transport.
@rtype: L{FakeTransport}
FrE   r;   )clientProtocols    r   makeFakeClientr      s     %88r   c                    [        U SS9$ )z
Create and return a new in-memory transport hooked up to the given protocol.

@param serverProtocol: The server protocol to use.
@type serverProtocol: L{IProtocol} provider

@return: The transport.
@rtype: L{FakeTransport}
Tr   r   )serverProtocols    r   makeFakeServerr     s     $77r   c                  6    \ rS rSrSrSS jrS	S jrS	S jrSrg)
IOPumpi  z
Utility to pump data between clients and servers for protocol testing.

Perhaps this is a utility worthy of being in protocol.py?
Nc                f    Xl         X l        X0l        X@l        XPl        Uc
  [        5       nX`l        g r$   )clientserverclientIOserverIOdebugr   clock)r   r   r   r   r   r   r   s          r   r   IOPump.__init__  s-      
=&(E
r   c                n    Sn[        S5       H  nU R                  X5      (       a  SnM    U$     S5       e)zS
Pump until there is no more input or output.

Returns whether any data was moved.
Fi  TzToo long)rangepump)r   r   advanceClockresult_s        r   flushIOPump.flush)  sA     tAyy--   !j 1r   c                   U(       a  U R                   R                  S5        U R                  (       d  U(       a  [        S5        U R                  R                  5       nU R                  R                  5       nU R                  R                  5         U R                  R                  5         U R                  (       d  U(       aG  [        S5        U(       a  [        S[        U5      -   5        U(       a  [        S[        U5      -   5        U(       a  U R                  R                  U5        U(       a  U R                  R                  U5        U(       d  U(       a  gU R                  R                  (       a{  U R                  R                  (       d`  U R                  (       d  U(       a  [        S5        SU R                  l        SU R                  l
        U R                  R                  5         gU R                  R                  (       a{  U R                  R                  (       d`  U R                  (       d  U(       a  [        S5        SU R                  l        SU R                  l
        U R                  R                  5         gg	)
z
Move data back and forth, while also triggering any currently pending
scheduled calls (i.e. C{callLater(0, f)}).

Returns whether any data was moved.
r   z
-- GLUG --.zC: zS: Tz* Cz* SF)r   advancer   printr   r   r   r_   reprr   rS   r   rz   )r   r   r   sDatacDatas        r   r   IOPump.pump9  s    JJq!::,**,**,$$&$$&::#Jed5k)*ed5k)*MM((/MM((/E==&&t}}/I/IzzUe)-DMM&*.DMM'MM**,==&&t}}/I/IzzUe)-DMM&*.DMM'MM**,r   )r   r   r   r   r   r   r$   FT)	r0   r1   r2   r3   r9   r   r   r   r4   r5   r   r   r   r     s     *r   r   FTc           	         U R                  U5        UR                  U5        [        UU UUUUS9nU(       a  UR                  5         U$ )as  
Create a new L{IOPump} connecting two protocols.

@param serverProtocol: The protocol to use on the accepting side of the
    connection.
@type serverProtocol: L{IProtocol} provider

@param serverTransport: The transport to associate with C{serverProtocol}.
@type serverTransport: L{FakeTransport}

@param clientProtocol: The protocol to use on the initiating side of the
    connection.
@type clientProtocol: L{IProtocol} provider

@param clientTransport: The transport to associate with C{clientProtocol}.
@type clientTransport: L{FakeTransport}

@param debug: A flag indicating whether to log information about what the
    L{IOPump} is doing.
@type debug: L{bool}

@param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
    returning to put the protocols into their post-handshake or
    post-server-greeting state?
@type greet: L{bool}

@param clock: An optional L{Clock}. Pumping the resulting L{IOPump} will
    also increase clock time by a small increment.

@return: An L{IOPump} which connects C{serverProtocol} and
    C{clientProtocol} and delivers bytes between them when it is pumped.
@rtype: L{IOPump}
r   )makeConnectionr   r   )r   serverTransportr   clientTransportr   greetr   r   s           r   connectr   f  sP    T !!/2!!/2D 

Kr   c                Z    U" 5       nU " 5       nU" U5      n	U" U5      n
Xx[        XXyXEUS94$ )a  
Connect a given server and client class to each other.

@param ServerClass: a callable that produces the server-side protocol.
@type ServerClass: 0-argument callable returning L{IProtocol} provider.

@param ClientClass: like C{ServerClass} but for the other side of the
    connection.
@type ClientClass: 0-argument callable returning L{IProtocol} provider.

@param clientTransportFactory: a callable that produces the transport which
    will be attached to the protocol returned from C{ClientClass}.
@type clientTransportFactory: callable taking (L{IProtocol}) and returning
    L{FakeTransport}

@param serverTransportFactory: a callable that produces the transport which
    will be attached to the protocol returned from C{ServerClass}.
@type serverTransportFactory: callable taking (L{IProtocol}) and returning
    L{FakeTransport}

@param debug: Should this dump an escaped version of all traffic on this
    connection to stdout for inspection?
@type debug: L{bool}

@param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
    returning to put the protocols into their post-handshake or
    post-server-greeting state?
@type greet: L{bool}

@param clock: An optional L{Clock}. Pumping the resulting L{IOPump} will
    also increase clock time by a small increment.

@return: the client protocol, the server protocol, and an L{IOPump} which,
    when its C{pump} and C{flush} methods are called, will move data
    between the created client and server protocol instances.
@rtype: 3-L{tuple} of L{IProtocol}, L{IProtocol}, L{IOPump}
r   )r   )ServerClassClientClassclientTransportFactoryserverTransportFactoryr   r   r   csciosios              r   connectedServerAndClientr     s=    \ 	AA
 
#C
 
#CUCCCr   c                .    U u  nnnnnUu  pxpXs:X  a  XH4$ g)a  
Should the client and server described by the arguments be connected to
each other, i.e. do their port numbers match?

@param clientInfo: the args for connectTCP
@type clientInfo: L{tuple}

@param serverInfo: the args for listenTCP
@type serverInfo: L{tuple}

@return: If they do match, return factories for the client and server that
    should connect; otherwise return L{None}, indicating they shouldn't be
    connected.
@rtype: L{None} or 2-L{tuple} of (L{ClientFactory},
    L{IProtocolFactory})
Nr5   )
clientInfo
serverInfo
clientHost
clientPortclientFactoryclientTimeoutclientBindAddress
serverPortserverFactoryserverBacklogserverInterfaces              r   _factoriesShouldConnectr     s7    . 	BL?Z++r   c                  V    \ rS rSrSrSS jrS	S
S jjr\" \" 5       5      4SS jjr	Sr
g)ConnectionCompleteri  z
A L{ConnectionCompleter} can cause synthetic TCP connections established by
L{MemoryReactor.connectTCP} and L{MemoryReactor.listenTCP} to succeed or
fail.
c                    Xl         g)zk
Create a L{ConnectionCompleter} from a L{MemoryReactor}.

@param memoryReactor: The reactor to attach to.
N_reactor)r   memoryReactors     r   r   ConnectionCompleter.__init__  s	     &r   c                   U R                   n[        UR                  5       H  u  pEUR                   H  n[	        XV5      nU(       d  M  UR                  R                  U5        UR                  R                  U5        Uu  pUR                  S5      n
U	R                  S5      n[        U5      n[        U
5      n[        UUU
UUUS9nUs  s  $    M     g)a  
Complete a single TCP connection established on this
L{ConnectionCompleter}'s L{MemoryReactor}.

@param debug: A flag; whether to dump output from the established
    connection to stdout.

@return: a pump for the connection, or L{None} if no connection could
    be established.
N)r   )r   	enumerate
tcpClients
tcpServersr   remove
connectorspopbuildProtocolr   r   r   )r   r   r   r   	clientIdxr   r   	factoriesr   r   r   r   r   r   r   s                  r   succeedOnceConnectionCompleter.succeedOnce  s     %.}/G/G%H!I+66
3JK	9!,,33J?!,,00;3<0M%2%@%@%FN%2%@%@%FN&4^&DO&4^&DO%,&'&'#&F "M% 7 &I( r   c                    U R                   R                  R                  S5      S   R                  U R                   R                  R                  S5      U5        g)z
Fail a single TCP connection established on this
L{ConnectionCompleter}'s L{MemoryReactor}.

@param reason: the reason to provide that the connection failed.
r      N)r   r  r  clientConnectionFailedr  )r   reasons     r   failOnceConnectionCompleter.failOnce%  sF     	  $$Q'*AAMM$$((+V	
r   r   N)r   r   r.   Noner   )r   boolr   r  r.   zIOPump | None)r  r   r.   r  )r0   r1   r2   r3   r9   r   r	  r   r   r  r4   r5   r   r   r   r     s*    & D *11G1I)J 	
 	
r   r   c                    [        5       n[        USS5      n[        US5      nUR                  [        R
                  " [        5      5        U[        U5      4$ )ae  
Create an endpoint that can be fired on demand.

@param debug: A flag; whether to dump output from the established
    connection to stdout.
@type debug: L{bool}

@return: A client endpoint, and an object that will cause one of the
    L{Deferred}s returned by that client endpoint.
@rtype: 2-L{tuple} of (L{IStreamClientEndpoint}, L{ConnectionCompleter})
z0.0.0.0i  )r   r	   r
   listenr   forProtocolr   r   )r   reactorclientEndpointserverEndpoints       r   connectableEndpointr  1  sP     !"G'DAN'6N'--h78.w777r   )FTN)F)*r9   
__future__r   r   OpenSSL.SSLr   r&   ImportErrorzope.interfacer   r   twisted.internetr   r   twisted.internet.endpointsr	   r
   twisted.internet.errorr   twisted.internet.protocolr   r   twisted.internet.testingr   r   twisted.python.failurer   r   IAddressr7   
ITransportITLSTransport	IConsumerr;   r   r   r   r   r   r   r   r  r5   r   r   <module>r(     s  
 # 	7 9 . M 9 7 F *! !& Z  !  " Z""J$<$<j>R>RSD D TDN
9
8K Kf 

7z *)


2Dj>:
 :
z8G  		s   C C$#C$