
    h2                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJrJrJr   " S	 S
\R                  5      r " S S\5      r " S S\5      rSrSr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S \5      r " S! S"\5      r " S# S$\5      r " S% S&\5      r " S' S(\R                  5      r  " S) S*\R                  5      r!/ S+Qr"g,)-a  
Reactor-based Services

Here are services to run clients, servers and periodic services using
the reactor.

If you want to run a server service, L{StreamServerEndpointService} defines a
service that can wrap an arbitrary L{IStreamServerEndpoint
<twisted.internet.interfaces.IStreamServerEndpoint>}
as an L{IService}. See also L{twisted.application.strports.service} for
constructing one of these directly from a descriptive string.

Additionally, this module (dynamically) defines various Service subclasses that
let you represent clients and servers in a Service hierarchy.  Endpoints APIs
should be preferred for stream server services, but since those APIs do not yet
exist for clients or datagram services, many of these are still useful.

They are as follows::

  TCPServer, TCPClient,
  UNIXServer, UNIXClient,
  SSLServer, SSLClient,
  UDPServer,
  UNIXDatagramServer, UNIXDatagramClient,
  MulticastServer

These classes take arbitrary arguments in their constructors and pass
them straight on to their respective reactor.listenXXX or
reactor.connectXXX calls.

For example, the following service starts a web server on port 8080:
C{TCPServer(8080, server.Site(r))}.  See the documentation for the
reactor.listen/connect* methods for more information.
    )List)service)task)CancelledError)log   )ClientService_maybeGlobalReactorbackoffPolicyc                   0    \ rS rSr% / r\\   \S'   S rSr	g)_VolatileDataService2   volatilec                 |    [         R                  R                  U 5      nU R                   H  nX!;   d  M
  X	 M     U$ N)r   Service__getstate__r   )selfdattrs      c/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/application/internet.pyr   !_VolatileDataService.__getstate__5   s5    OO((.MMDyG "      N)
__name__
__module____qualname____firstlineno__r   r   str__annotations__r   __static_attributes__r   r   r   r   r   2   s    Hd3ir   r   c                   T    \ rS rSr% SrS/rSr\\S'   Sr	Sr
S rS rS	 rS
 rS rSrg)_AbstractServer=   a  
@cvar volatile: list of attribute to remove from pickling.
@type volatile: C{list}

@ivar method: the type of method to call on the reactor, one of B{TCP},
    B{UDP}, B{SSL} or B{UNIX}.
@type method: C{str}

@ivar reactor: the current running reactor.
@type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
    C{IReactorSSL} or C{IReactorUnix}.

@ivar _port: instance of port set when the service is started.
@type _port: a provider of L{twisted.internet.interfaces.IListeningPort}.
_port methodNc                 T    Xl         SU;   a  UR                  S5      U l        X l        g Nreactorargspopr*   kwargsr   r,   r.   s      r   __init___AbstractServer.__init__T   $    	!::i0DLr   c                 l    [         R                  R                  U 5        U R                  5       U l        g r   )r   r   privilegedStartService_getPortr%   r   s    r   r4   &_AbstractServer.privilegedStartServiceZ   s!    ..t4]]_
r   c                     [         R                  R                  U 5        U R                  c  U R	                  5       U l        g g r   )r   r   startServicer%   r5   r6   s    r   r9   _AbstractServer.startService^   s0    $$T*::DJ r   c                     [         R                  R                  U 5        U R                  b  U R                  R	                  5       nU ?U$ g r   )r   r   stopServicer%   stopListening)r   r   s     r   r<   _AbstractServer.stopServicec   sA    ##D) ::!

((*A
H "r   c                     [        [        U R                  5      SR                  U R                  5      5      " U R
                  0 U R                  D6$ )z
Wrapper around the appropriate listen method of the reactor.

@return: the port object returned by the listen method.
@rtype: an object providing
    L{twisted.internet.interfaces.IListeningPort}.
zlisten{})getattrr
   r*   formatr'   r,   r.   r6   s    r   r5   _AbstractServer._getPortl   sN     -

 99%
 % 	%r   )r%   r,   r.   r*   )r   r   r   r   __doc__r   r'   r   r    r*   r%   r0   r4   r9   r<   r5   r!   r   r   r   r#   r#   =   s=      yHFCGE%)
%r   r#   c                   N    \ rS rSr% SrS/rSr\\S'   Sr	Sr
S rS rS	 rS
 rSrg)_AbstractClient|   a  
@cvar volatile: list of attribute to remove from pickling.
@type volatile: C{list}

@ivar method: the type of method to call on the reactor, one of B{TCP},
    B{UDP}, B{SSL} or B{UNIX}.
@type method: C{str}

@ivar reactor: the current running reactor.
@type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
    C{IReactorSSL} or C{IReactorUnix}.

@ivar _connection: instance of connection set when the service is started.
@type _connection: a provider of L{twisted.internet.interfaces.IConnector}.
_connectionr&   r'   Nc                 T    Xl         SU;   a  UR                  S5      U l        X l        g r)   r+   r/   s      r   r0   _AbstractClient.__init__   r2   r   c                 l    [         R                  R                  U 5        U R                  5       U l        g r   )r   r   r9   _getConnectionrG   r6   s    r   r9   _AbstractClient.startService   s%    $$T*..0r   c                     [         R                  R                  U 5        U R                  b  U R                  R	                  5         U ?g g r   )r   r   r<   rG   
disconnectr6   s    r   r<   _AbstractClient.stopService   s=    ##D)''')  (r   c                     [        [        U R                  5      SU R                   35      " U R                  0 U R
                  D6$ )z
Wrapper around the appropriate connect method of the reactor.

@return: the port object returned by the connect method.
@rtype: an object providing L{twisted.internet.interfaces.IConnector}.
connect)r@   r
   r*   r'   r,   r.   r6   s    r   rK   _AbstractClient._getConnection   s@     *4<<8GDKK=:QRYY
++
 	
r   )rG   r,   r.   r*   )r   r   r   r   rC   r   r'   r   r    r*   rG   r0   r9   r<   rK   r!   r   r   r   rE   rE   |   s8      HFCGK1!	
r   rE   ztConnect to {tran}

Call reactor.connect{tran} when the service starts, with the
arguments given to the constructor.
zServe {tran} clients

Call reactor.listen{tran} when the service starts, with the
arguments given to the constructor. When the service stops,
stop listening. See twisted.internet.interfaces for documentation
on arguments to the reactor method.
c                   6    \ rS rSr\R                  SS9rSrSrg)	TCPServer   TCPtranr   N	r   r   r   r   
_serverDocrA   rC   r'   r!   r   r   r   rT   rT          U+GFr   rT   c                   6    \ rS rSr\R                  SS9rSrSrg)	TCPClient   rV   rW   r   N	r   r   r   r   
_clientDocrA   rC   r'   r!   r   r   r   r]   r]      r[   r   r]   c                   6    \ rS rSr\R                  SS9rSrSrg)
UNIXServer   UNIXrW   r   NrY   r   r   r   rb   rb          V,GFr   rb   c                   6    \ rS rSr\R                  SS9rSrSrg)
UNIXClient   rd   rW   r   Nr_   r   r   r   rg   rg      re   r   rg   c                   6    \ rS rSr\R                  SS9rSrSrg)	SSLServer   SSLrW   r   NrY   r   r   r   rj   rj      r[   r   rj   c                   6    \ rS rSr\R                  SS9rSrSrg)	SSLClient   rl   rW   r   Nr_   r   r   r   rn   rn      r[   r   rn   c                   6    \ rS rSr\R                  SS9rSrSrg)	UDPServer   UDPrW   r   NrY   r   r   r   rq   rq      r[   r   rq   c                   6    \ rS rSr\R                  SS9rSrSrg)UNIXDatagramServer   UNIXDatagramrW   r   NrY   r   r   r   ru   ru          ^4GFr   ru   c                   6    \ rS rSr\R                  SS9rSrSrg)UNIXDatagramClient   rw   rW   r   Nr_   r   r   r   rz   rz      rx   r   rz   c                   6    \ rS rSr\R                  SS9rSrSrg)MulticastServer   	MulticastrW   r   NrY   r   r   r   r}   r}      s    [1GFr   r}   c                   8    \ rS rSrSrSS/rS rS rS rS r	S	r
g
)TimerService   aZ  
Service to periodically call a function

Every C{step} seconds call the given function with the given arguments.
The service starts the calls when it starts, and cancels them
when it stops.

@ivar clock: Source of time. This defaults to L{None} which is
    causes L{twisted.internet.reactor} to be used.
    Feel free to set this to something else, but it probably ought to be
    set *before* calling L{startService}.
@type clock: L{IReactorTime<twisted.internet.interfaces.IReactorTime>}

@ivar call: Function and arguments to call periodically.
@type call: L{tuple} of C{(callable, args, kwargs)}
_loop_loopFinishedc                 0    Xl         X#U4U l        SU l        g)z
@param step: The number of seconds between calls.
@type step: L{float}

@param callable: Function to call
@type callable: L{callable}

@param args: Positional arguments to pass to function
@param kwargs: Keyword arguments to pass to function
N)stepcallclock)r   r   callabler,   r.   s        r   r0   TimerService.__init__  s     	V,	
r   c                    [         R                  R                  U 5        U R                  u  pn[        R
                  " U/UQ70 UD6U l        [        U R                  5      U R                  l        U R                  R                  U R                  SS9U l        U R                  R                  U R                  5        g )NT)now)r   r   r9   r   r   LoopingCallr   r
   r   startr   r   
addErrback_failed)r   r   r,   r.   s       r   r9   TimerService.startService  s    $$T*!%
 %%h@@@
.tzz:

!ZZ--diiT-B%%dll3r   c                 R    SU R                   l        [        R                  " U5        g NF)r   runningr   err)r   whys     r   r   TimerService._failed  s     #

r   c                    ^  T R                   R                  (       a  T R                   R                  5         T R                  R	                  U 4S j5        T R                  $ )z
Stop the service.

@rtype: L{Deferred<defer.Deferred>}
@return: a L{Deferred<defer.Deferred>} which is fired when the
    currently running call (if any) is finished.
c                 B   > [         R                  R                  T5      $ r   )r   r   r<   )_r   s    r   <lambda>*TimerService.stopService.<locals>.<lambda>0  s    1L1LT1Rr   )r   r   stopr   addCallbackr6   s   `r   r<   TimerService.stopService&  sA     ::JJOO&&'RS!!!r   )r   r   r   r   r   N)r   r   r   r   rC   r   r0   r9   r   r<   r!   r   r   r   r   r      s'    " )H
4"r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	CooperatorServicei4  zZ
Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
c                 6    [         R                  " SS9U l        g )NF)started)r   
Cooperatorcoopr6   s    r   r0   CooperatorService.__init__9  s    OOE2	r   c                 8    U R                   R                  U5      $ r   )r   	coiterate)r   iterators     r   r   CooperatorService.coiterate<  s    yy""8,,r   c                 8    U R                   R                  5         g r   )r   r   r6   s    r   r9   CooperatorService.startService?  s    		r   c                 8    U R                   R                  5         g r   )r   r   r6   s    r   r<   CooperatorService.stopServiceB  s    		r   )r   N)
r   r   r   r   rC   r0   r   r9   r<   r!   r   r   r   r   r   4  s    3-r   r   c                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)
StreamServerEndpointServiceiF  a  
A L{StreamServerEndpointService} is an L{IService} which runs a server on a
listening port described by an L{IStreamServerEndpoint
<twisted.internet.interfaces.IStreamServerEndpoint>}.

@ivar factory: A server factory which will be used to listen on the
    endpoint.

@ivar endpoint: An L{IStreamServerEndpoint
    <twisted.internet.interfaces.IStreamServerEndpoint>} provider
    which will be used to listen when the service starts.

@ivar _waitingForPort: a Deferred, if C{listen} has yet been invoked on the
    endpoint, otherwise None.

@ivar _raiseSynchronously: Defines error-handling behavior for the case
    where C{listen(...)} raises an exception before C{startService} or
    C{privilegedStartService} have completed.

@type _raiseSynchronously: C{bool}

@since: 10.2
Fc                 *    Xl         X l        S U l        g r   )endpointfactory_waitingForPort)r   r   r   s      r   r0   $StreamServerEndpointService.__init__a  s     #r   c                 $  ^ ^ [         R                  R                  T 5        T R                  R	                  T R
                  5      T l        / mUU 4S jnT R                  R                  U5        T(       a  TS   R                  5         ST l	        g)z"
Start listening on the endpoint.
c                    > TR                   (       a  TR                  U 5        g U R                  [        5      (       d  [        R
                  " U 5        g g r   )_raiseSynchronouslyappendcheckr   r   r   )r   	raisedNowr   s    r   handleItDStreamServerEndpointService.privilegedStartService.<locals>.handleItn  s9    ''  %YY~.. /r   r   FN)
r   r   r4   r   listenr   r   r   raiseExceptionr   )r   r   r   s   ` @r   r4   2StreamServerEndpointService.privilegedStartServicef  sm     	..t4#}}33DLLA		 	''1aL'')#( r   c                 ~    [         R                  R                  U 5        U R                  c  U R	                  5         gg)z]
Start listening on the endpoint, unless L{privilegedStartService} got
around to it already.
N)r   r   r9   r   r4   r6   s    r   r9   (StreamServerEndpointService.startServicey  s3    
 	$$T*''') (r   c                    ^  T R                   R                  5         S nT R                   R                  U5      nU 4S jnUR                  U5        U$ )z
Stop listening on the port if it is already listening, otherwise,
cancel the attempt to listen.

@return: a L{Deferred<twisted.internet.defer.Deferred>} which fires
    with L{None} when the port has stopped listening.
c                 *    U b  U R                  5       $ g r   )r=   )ports    r   stopIt7StreamServerEndpointService.stopService.<locals>.stopIt  s    ))++  r   c                    > STl         U $ r   )r   )passthroughr   s    r   r   5StreamServerEndpointService.stopService.<locals>.stop  s     DLr   )r   cancelr   addBoth)r   r   r   r   s   `   r   r<   'StreamServerEndpointService.stopService  sK     	##%	,   ,,V4	 	
		$r   )r   r   r   r   r   N)r   r   r   r   rC   r   r0   r4   r9   r<   r!   r   r   r   r   r   F  s#    0  $
)&*r   r   )r   r   r}   r   rq   r	   rT   r]   rb   rg   rj   rn   ru   rz   r	   r   N)#rC   typingr   twisted.applicationr   twisted.internetr   twisted.internet.deferr   twisted.pythonr   _client_servicer	   r
   r   r   r   r#   rE   r`   rZ   rT   r]   rb   rg   rj   rn   rq   ru   rz   r}   r   r   r   __all__r   r   r   <module>r      s  
!H  ' ! 1  N N7?? <%* <%~0
* 0
f

 
 
 
 
 
 
 
 
 
o 
A"' A"H $Q'// Qhr   