
    hB                       S r SSKJr  SSKrSSK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  \S
:X  a3  SSKJr  SSKJr  SSKJrJrJrJrJrJrJr  \\\\/r\\\\/r\r\r\r \r!OSSKJ r JrJ!r!JrJrJ"r"  \ \!\"/r\/rSSK#J$r$J%r%J&r&J'r'J(r(J)r)  SSK*J+r+  \" \)RX                  \)RZ                  \)R\                  5       " S S\&R^                  5      5       r0\" \)Rb                  5       " S S\
\05      5       r2g)a  
Various asynchronous UDP classes.

Please do not use this module directly.

@var _sockErrReadIgnore: list of symbolic error constants (from the C{errno}
    module) representing socket errors where the error is temporary and can be
    ignored.

@var _sockErrReadRefuse: list of symbolic error constants (from the C{errno}
    module) representing socket errors that indicate connection refused.
    )annotationsN)Optional)implementer)MulticastMixin)IReactorMulticast)AbstractDatagramProtocol)platformTypewin32)WSAEINPROGRESS)WSAEWOULDBLOCK)WSAECONNREFUSEDWSAECONNRESETWSAEINTRWSAEMSGSIZEWSAENETRESETWSAENOPROTOOPTWSAETIMEDOUT)EAGAINECONNREFUSEDEINTREMSGSIZEENOPROTOOPTEWOULDBLOCK)abstractaddressbasedefererror
interfaces)logc                     \ rS rSr% Sr\R                  rS\S'   \R                  r
S\S'   SrSrS	\S
'   SrS!S jr\S 5       rS"S j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#S jrS rS rS r S r!S r"S r#S r$g)$Port@   ay  
UDP port, listening for packets.

@ivar maxThroughput: Maximum number of bytes read in one event
    loop iteration.

@ivar addressFamily: L{socket.AF_INET} or L{socket.AF_INET6}, depending on
    whether this port is listening on an IPv4 address or an IPv6 address.

@ivar _realPortNumber: Actual port number being listened on. The
    value will be L{None} until this L{Port} is listening.

@ivar _preexistingSocket: If not L{None}, a L{socket.socket} instance which
    was created and initialized outside of the reactor and will be used to
    listen for connections (instead of a new socket being created by this
    L{Port}).
zsocket.AddressFamilyaddressFamilyzsocket.SocketKind
socketTypei   NzOptional[int]_realPortNumberc                    [         R                  R                  X5        Xl        X l        X@l        X0l        U R                  5         SU l        U R                  5         g)a  
@param port: A port number on which to listen.
@type port: L{int}

@param proto: A C{DatagramProtocol} instance which will be
    connected to the given C{port}.
@type proto: L{twisted.internet.protocol.DatagramProtocol}

@param interface: The local IPv4 or IPv6 address to which to bind;
    defaults to '', ie all IPv4 addresses.
@type interface: L{str}

@param maxPacketSize: The maximum packet size to accept.
@type maxPacketSize: L{int}

@param reactor: A reactor which will notify this C{Port} when
    its socket is ready for reading or writing. Defaults to
    L{None}, ie the default global reactor.
@type reactor: L{interfaces.IReactorFDSet}
N)
r   BasePort__init__portprotocolmaxPacketSize	interface	setLogStr_connectedAddr_setAddressFamily)selfr*   protor-   r,   reactors         [/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/udp.pyr)   Port.__init__]   sI    * 	t-	*""     c                    [         R                  " X#U R                  5      nUR                  5       S   nU " SUUUUS9nXhl        U$ )a  
Create a new L{Port} based on an existing listening
I{SOCK_DGRAM} socket.

@param reactor: A reactor which will notify this L{Port} when
    its socket is ready for reading or writing. Defaults to
    L{None}, ie the default global reactor.
@type reactor: L{interfaces.IReactorFDSet}

@param fd: An integer file descriptor associated with a listening
    socket.  The socket must be in non-blocking mode.  Any additional
    attributes desired, such as I{FD_CLOEXEC}, must also be set already.
@type fd: L{int}

@param addressFamily: The address family (sometimes called I{domain}) of
    the existing socket.  For example, L{socket.AF_INET}.
@type addressFamily: L{int}

@param protocol: A C{DatagramProtocol} instance which will be
    connected to the C{port}.
@type protocol: L{twisted.internet.protocol.DatagramProtocol}

@param maxPacketSize: The maximum packet size to accept.
@type maxPacketSize: L{int}

@return: A new instance of C{cls} wrapping the socket given by C{fd}.
@rtype: L{Port}
r   N)r-   r3   r,   )socketfromfdr%   getsockname_preexistingSocket)	clsr3   fdr$   r+   r,   r*   r-   r1   s	            r4   _fromListeningDescriptorPort._fromListeningDescriptor{   sR    @ }}R?$$&q)	'
 #'r6   c                    U R                   b'  SU R                  R                   SU R                    S3$ SU R                  R                   S3$ )N<z on >z not connected>)r&   r+   	__class__r1   s    r4   __repr__Port.__repr__   sO    +t}}../tD4H4H3IKKt}}../??r6   c                    U R                   $ )z
Return a socket object.
)r8   rD   s    r4   	getHandlePort.getHandle        {{r6   c                D    U R                  5         U R                  5         g)z
Create and bind my socket, and begin listening on it.

This is called on unserialization, and must be called after creating a
server to begin listening on the specified port.
N)_bindSocket_connectToProtocolrD   s    r4   startListeningPort.startListening   s     	!r6   c                2   U R                   c9   U R                  5       nUR                  U R                  U R                  45        OU R                   nSU l         UR                  5       S   U l	        [        R                  " U R                  U R                  5      < SU R                  < 35        SU l        Xl        U R                  R                   U l        g! [
         a1  n[        R                  " U R                  U R                  U5      eSnAff = f)a  
Prepare and assign a L{socket.socket} instance to
C{self.socket}.

Either creates a new SOCK_DGRAM L{socket.socket} bound to
C{self.interface} and C{self.port} or takes an existing
L{socket.socket} provided via the
L{interfaces.IReactorSocket.adoptDatagramPort} interface.
N   z starting on )r;   createInternetSocketbindr-   r*   OSErrorr   CannotListenErrorr:   r&   r    msg_getLogPrefixr+   	connectedr8   filenor1   sktles      r4   rL   Port._bindSocket   s     ""*M//1$..$))45
 ))C&*D#  #03!!$--0$2F2FH	

 kk((%  M--dnndiiLLMs   7C 
D%,DDc                Z    U R                   R                  U 5        U R                  5         g N)r+   makeConnectionstartReadingrD   s    r4   rM   Port._connectToProtocol   s     $$T*r6   c                T   SnXR                   :  a   U R                  R                  U R                  5      u  p#U[	        U5      -  nU R
                  [        R                  :X  a  USS n U R                  R                  X#5        XR                   :  a  M  gg! [         a    [        R                  " 5          N4f = f! [         a^  nUR                  S   nU[        ;   a   SnAgU[        ;   a0  U R                   (       a  U R                  R#                  5          SnAge SnAff = f)z-
Called when my socket is ready for reading.
r   N   )maxThroughputr8   recvfromr,   lenr$   AF_INET6r+   datagramReceivedBaseExceptionr    errrT   args_sockErrReadIgnore_sockErrReadRefuser/   connectionRefused)r1   readdataaddrsenos         r4   doReadPort.doRead   s     '''![[11$2D2DE
 D	!%%8  8DMM224>/ '''0 % GGI+  WWQZ++++**779s5   'B? ,B B<;B<?
D'	D"'5D"!D""D'c                   U R                   (       a0  USU R                   4;   d   e U R                  R                  U5      $ US:w  d   e[        R                  " US   5      (       dA  [        R                  " US   5      (       d#  US   S:w  a  [        R                   " US   S5      e[        R                  " US   5      (       d	  US   S:X  a8  U R"                  [        R$                  :X  a  [        R                   " US   S5      e[        R                  " US   5      (       a8  U R"                  [        R&                  :X  a  [        R                   " US   S5      e U R                  R)                  X5      $ ! [         a~  nUR                  S   nU[
        :X  a  U R                  U5      s SnA$ U[        :X  a  [        R                  " S5      eU[        :X  a  U R                  R                  5          SnAge SnAff = f! [         ad  nUR                  S   nU[
        :X  a  U R                  X5      s SnA$ U[        :X  a  [        R                  " S5      eU[        :X  a   SnAge SnAff = f)a:  
Write a datagram.

@type datagram: L{bytes}
@param datagram: The datagram to be sent.

@type addr: L{tuple} containing L{str} as first element and L{int} as
    second element, or L{None}
@param addr: A tuple of (I{stringified IPv4 or IPv6 address},
    I{integer port number}); can be L{None} in connected mode.
Nr   zmessage too longz<broadcast>z0write() only accepts IP addresses, not hostnamesz7IPv6 port write() called with IPv4 or broadcast addressz*IPv4 port write() called with IPv6 address)r/   r8   sendrT   rl   r   writer   r   MessageLengthErrorr   r+   ro   r   isIPAddressisIPv6AddressInvalidAddressErrorr$   rh   AF_INETsendto)r1   datagramrr   rs   rt   s        r4   ry   
Port.write  s    D$"5"56666{{''11 4<<((a11 ..tAw77G},//GO  $$T!W--aM1I$$7//GV  %%d1g..43E3E3W//GI {{))(99?  	WWQZ;::h//8^223EFF<'MM3355	@  WWQZ;::h558^223EFF<' sT   E: H :
H)G=-H3AG=<G==H
I3)I.8I3>*I.-I..I3c                F    U R                  SR                  U5      U5        g)a  
Write a datagram constructed from an iterable of L{bytes}.

@param seq: The data that will make up the complete datagram to be
    written.
@type seq: an iterable of L{bytes}

@type addr: L{tuple} containing L{str} as first element and L{int} as
    second element, or L{None}
@param addr: A tuple of (I{stringified IPv4 or IPv6 address},
    I{integer port number}); can be L{None} in connected mode.
r6   N)ry   join)r1   seqrr   s      r4   writeSequencePort.writeSequenceB  s     	

388C=$'r6   c                   U R                   (       a  [        S5      e[        R                  " U5      (       d2  [        R                  " U5      (       d  [
        R                  " US5      eX4U l         U R                  R                  X45        g)z
'Connect' to remote server.
z:already connected, reconnecting is not currently supportednot an IPv4 or IPv6 address.N)	r/   RuntimeErrorr   r{   r|   r   r}   r8   connect)r1   hostr*   s      r4   r   Port.connectQ  sr     L  ##D))(2H2H2N2N++D2PQQ#lTL)r6   c                    U R                  5         U R                  (       a'  U R                  R                  SU R                  5        g g )Nr   )stopReadingrX   r3   	callLaterconnectionLostrD   s    r4   _loseConnectionPort._loseConnection^  s4    >>LL""1d&9&9: r6   c                    U R                   (       a  [        R                  " 5       =ol        OS nU R	                  5         U$ r_   )rX   r   Deferreddr   )r1   results     r4   stopListeningPort.stopListeningc  s0    >>#nn..FVFr6   c                X    [         R                  " S[        SS9  U R                  5         g )Nz-Please use stopListening() to disconnect portrd   )
stacklevel)warningswarnDeprecationWarningr   rD   s    r4   loseConnectionPort.loseConnectionk  s%    ;	

 	r6   c                r   [         R                  " SU R                  -  5        SU l        SU l        [        R
                  R                  X5        U R                  R                  5         U R                  R                  5         U ?	U ?[        U S5      (       a  U R                  R                  S5        U ?gg)z
Cleans up my socket.
z(UDP Port %s Closed)Nr   )r    rV   r&   re   r   r(   r   r+   doStopr8   closerY   hasattrr   callback)r1   reasons     r4   r   Port.connectionLosts  s     	&)=)==>#$$T2KK4FFOOD! r6   c                N    U R                  U R                  5      nSU-  U l        g)z@
Initialize the C{logstr} attribute to be used by C{logPrefix}.
z%s (UDP)N)rW   r+   logstr)r1   	logPrefixs     r4   r.   Port.setLogStr  s$     &&t}}5	 9,r6   c                T   [         R                  " U R                  5      (       a  [        R                  U l        g[         R                  " U R                  5      (       a  [        R                  U l        gU R                  (       a!  [        R                  " U R                  S5      eg)z(
Resolve address family for the socket.
r   N)
r   r|   r-   r8   rh   r$   r{   r~   r   r}   rD   s    r4   r0   Port._setAddressFamily  so     !!$..11!'D!!$..11!'D^^++ >  r6   c                    U R                   $ )z 
Return the prefix to log with.
)r   rD   s    r4   r   Port.logPrefix  rJ   r6   c                   U R                   R                  5       nU R                  [         R                  :X  a  [        R
                  " S/UQ76 $ U R                  [         R                  :X  a  [        R                  " S/USS Q76 $ g)z
Return the local address of the UDP connection

@returns: the local address of the UDP connection
@rtype: L{IPv4Address} or L{IPv6Address}
UDPNrd   )r8   r:   r$   r~   r   IPv4Addressrh   IPv6Address)r1   rr   s     r4   getHostPort.getHost  sq     {{&&(/&&u4t446??2&&u:Ra:: 3r6   c                v    U R                   R                  [         R                  [         R                  U5        g)z
Set whether this port may broadcast. This is disabled by default.

@param enabled: Whether the port may broadcast.
@type enabled: L{bool}
N)r8   
setsockopt
SOL_SOCKETSO_BROADCAST)r1   enableds     r4   setBroadcastAllowedPort.setBroadcastAllowed  s&     	v00&2E2EwOr6   c                    [        U R                  R                  [        R                  [        R                  5      5      $ )zs
Checks if broadcast is currently allowed on this port.

@return: Whether this port may broadcast.
@rtype: L{bool}
)boolr8   
getsockoptr   r   rD   s    r4   getBroadcastAllowedPort.getBroadcastAllowed  s,     DKK**6+<+<f>Q>QRSSr6   )r/   r;   r&   r$   rX   r   rY   r-   r   r,   re   r*   r+   r8   )     N)returnstrr_   )%__name__
__module____qualname____firstlineno____doc__r8   r~   r$   __annotations__
SOCK_DGRAMr%   re   r&   r;   r)   classmethodr>   rE   rH   rN   rL   rM   ru   ry   r   r   r   r   r   r   r.   r0   r   r   r   r   __static_attributes__ r6   r4   r"   r"   @   s    $ +1..M'8$*$5$5J!5M%)O])!< ) )V@"!)F@<|(*;
 -;PTr6   r"   c                  R    \ rS rSrSr    S             SS jjrS	S jrSrg)
MulticastPorti  z&
UDP Port that supports multicasting.
Nc                >    [         R                  XX#XE5        X`l        g)zH
@see: L{twisted.internet.interfaces.IReactorMulticast.listenMulticast}
N)r"   r)   listenMultiple)r1   r*   r2   r-   r,   r3   r   s          r4   r)   MulticastPort.__init__  s     	d%MK,r6   c                   [         R                  U 5      nU R                  (       av  UR                  [        R
                  [        R                  S5        [        [        S5      (       a2   UR                  [        R
                  [        R                  S5        U$ U$ ! [         a   nUR                  [        :X  a   SnAU$ e SnAff = f)z
Override L{Port.createInternetSocket} to configure the socket to honor
the C{listenMultiple} argument to L{IReactorMulticast.listenMultiple}.
rQ   SO_REUSEPORTN)r"   rR   r   r   r8   r   SO_REUSEADDRr   r   rT   errnor   rZ   s      r4   rR   "MulticastPort.createInternetSocket  s    
 ''-NN6,,f.A.A1Ev~..NN6#4#4f6I6I1M 
s
  xx;. 
 s   ,/B 
C	)CCC	)r   )r   r   NF)r*   intr2   r   r-   r   r,   r   r3   zIReactorMulticast | Noner   r   r   None)r   zsocket.socket)r   r   r   r   r   r)   rR   r   r   r6   r4   r   r     sc     !,0$-- (- 	-
 - *- - 
-r6   r   )3r   
__future__r   r8   r   typingr   zope.interfacer   twisted.internet._multicastr   twisted.internet.interfacesr   twisted.internet.protocolr   twisted.python.runtimer	   r   r   r   r   r   r   r   r   r   r   r   rm   rn   r   r   r   r   r   twisted.internetr   r   r   r   r   r   twisted.pythonr    IListeningPortIUDPTransportISystemHandler(   r"   IMulticastTransportr   r   r6   r4   <module>r      s  
 #    & 6 9 > /7$$   #NKP)=,U H"LFEUU %5& O N  z779Q9QwT4== wTwTt Z++,%ND % -%r6   