
    h#                         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
  S SKJr   " S S5      r\" \R                  5       " S	 S
5      5       rg)    N)implementer)error
interfaces)clientdns)DNSNameError)nativeStringc                   2    \ rS rSrS rS rS rS rS rSr	g)	"_SRVConnector_ClientFactoryWrapper   c                     Xl         X l        g N)-_SRVConnector_ClientFactoryWrapper__connector2_SRVConnector_ClientFactoryWrapper__wrappedFactory)self	connectorwrappedFactorys      _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/names/srvconnect.py__init__+_SRVConnector_ClientFactoryWrapper.__init__   s    $ .    c                 N    U R                   R                  U R                  5        g r   )r   startedConnectingr   )r   r   s     r   r   4_SRVConnector_ClientFactoryWrapper.startedConnecting   s    //0@0@Ar   c                 :    U R                   R                  U5        g r   )r   connectionFailedr   r   reasons      r   clientConnectionFailed9_SRVConnector_ClientFactoryWrapper.clientConnectionFailed   s    ))&1r   c                 :    U R                   R                  U5        g r   )r   connectionLostr   s      r   clientConnectionLost7_SRVConnector_ClientFactoryWrapper.clientConnectionLost   s    ''/r   c                 .    [        U R                  U5      $ r   )getattrr   )r   keys     r   __getattr__._SRVConnector_ClientFactoryWrapper.__getattr__   s    t,,c22r   )__connector__wrappedFactoryN)
__name__
__module____qualname____firstlineno__r   r   r   r#   r(   __static_attributes__ r   r   r   r      s    /B203r   r   c                   r    \ rS rSrSrSrSSS0 S4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g)SRVConnector"   a  
A connector that looks up DNS SRV records.

RFC 2782 details how SRV records should be interpreted and selected
for subsequent connection attempts. The algorithm for using the records'
priority and weight is implemented in L{pickServer}.

@ivar servers: List of candidate server records for future connection
    attempts.
@type servers: L{list} of L{dns.Record_SRV}

@ivar orderedServers: List of server records that have already been tried
    in this round of connection attempts.
@type orderedServers: L{list} of L{dns.Record_SRV}
r   tcp
connectTCPr1   Nc
                     Xl         X l        Uc  SO[        R                  " U5      U l        X@l        XPl        X`l        Xpl        Xl	        Xl
        SU l        SU l        SU l        g)a  
@param domain: The domain to connect to.  If passed as a text
    string, it will be encoded using C{idna} encoding.
@type domain: L{bytes} or L{str}

@param defaultPort: Optional default port number to be used when SRV
    lookup fails and the service name is unknown. This should be the
    port number associated with the service name as defined by the IANA
    registry.
@type defaultPort: L{int}
N)reactorservicer   domainStringdomainfactoryprotocolconnectFuncNameconnectFuncArgsconnectFuncKwArgs_defaultPortr   serversorderedServers)
r   r8   r9   r;   r<   r=   r>   r?   r@   defaultPorts
             r   r   SRVConnector.__init__6   s^    . $nd#2B2B62J ..!2'"r   c           
      $   U R                   R                  5         U R                   R                  U 5        U R                  (       Gd  U R                  c&  U R                  [        R                  " S5      5        g[        R                  " S[        U R                  5      < S[        U R                  5      < S[        U R                  5      < 35      nUR                  U R                  U R                  5        UR!                  U 4S j5        U R"                  (       a  UR%                  U R&                  5        UR%                  U R
                  5        gU R(                  c  U R+                  5         gU R(                  R-                  5         g)z"Start connection to remote server.NzDomain is not defined._z._.c                 "    UR                  5       $ r   )_reallyConnect)xr   s     r   <lambda>&SRVConnector.connect.<locals>.<lambda>q   s    t/B/B/Dr   )r<   doStartr   rB   r;   r   r   DNSLookupErrorr   lookupServicer	   r9   r=   addCallbacks_cbGotServers_ebGotServersaddCallbackrA   
addErrback_ebServiceUnknownr   rJ   connect)r   ds     r   rW   SRVConnector.connect]   s   &&t,|||{{"%%(()AB $$ !. / -	A NN4--t/A/ABMMDE  T334LL../^^#!NN""$r   c                 J    UR                  [        5        / U l        / U l        g r   )trapr   rB   rC   r   failures     r   rS   SRVConnector._ebGotServersz   s    \"  r   c                 \   Uu  p#n[        U5      S:X  a  US   R                  [        R                  :X  a  US   R                  (       av  US   R                  R
                  [        R                  " S5      :X  aE  [        R                  " S[        U R                  5      < S[        U R                  5      < S35      e/ U l        / U l        U HY  nUR                  [        R                  :w  d  UR                  (       d  M4  U R                  R                  UR                  5        M[     g )N   r      .zService z not available for domain rH   )lentyper   SRVpayloadtargetNamer   rO   reprr9   r;   rB   rC   append)r   resultanswersauthaddas         r   rR   SRVConnector._cbGotServers   s    #sLA
377*
""
""))SXXd^; &&%tDKK'8: 
  Avv 		&&qyy1	 r   c                     UR                  [        R                  5        [        R                  " SSU R
                  U R                  5      /U l        / U l        U R                  5         g)z
Connect to the default port when the service name is unknown.

If no SRV records were found, the service name will be passed as the
port. If resolving the name fails with
L{error.ServiceNameUnknownError}, a final attempt is done using the
default port.
r   N)
r[   r   ServiceNameUnknownErrorr   
Record_SRVrA   r;   rB   rC   rW   r\   s     r   rV   SRVConnector._ebServiceUnknown   sJ     	U223q!T->->LM r   c                    U R                   c   eU R                  c   eU R                   (       d2  U R                  (       d!  [        U R                  5      U R                  4$ U R                   (       d)  U R                  (       a  U R                  U l         / U l        U R                   (       d   eU R                   R                  S S9  U R                   S   R                  nSnSn/ nU R                    H:  nUR                  U:X  d  M  X5R                  -  nUR                  X#45        US-  nM<     [        R                  " SU5      nU Hd  u  p'Xv:  d  M  U R                   U   nU R                   U	 U R                  R                  U5        [        UR                  5      UR                  4s  $    [        SU R                  R                    S35      e)a  
Pick the next server.

This selects the next server from the list of SRV records according
to their priority and weight values, as set out by the default
algorithm specified in RFC 2782.

At the beginning of a round, L{servers} is populated with
L{orderedServers}, and the latter is made empty. L{servers}
is the list of candidates, and L{orderedServers} is the list of servers
that have already been tried.

First, all records are ordered by priority and weight in ascending
order. Then for each priority level, a running sum is calculated
over the sorted list of records for that priority. Then a random value
between 0 and the final sum is compared to each record in order. The
first record that is greater than or equal to that random value is
chosen and removed from the list of candidates for this round.

@return: A tuple of target hostname and port from the chosen DNS SRV
    record.
@rtype: L{tuple} of native L{str} and L{int}
c                 2    U R                   U R                  4$ r   )priorityweight)records    r   rL   )SRVConnector.pickServer.<locals>.<lambda>   s    foov}}-Mr   )r'   r   r`   zImpossible z pickServer result.)rB   rC   r	   r;   r9   sortrv   rw   ri   randomrandintstrrf   portRuntimeError	__class__r,   )	r   minPriorityindex	weightSumweightIndexrK   randrw   chosens	            r   
pickServerSRVConnector.pickServer   s   0 ||'''""...||D$7$7,dll::|| 3 3..DL"$D|||MNll1o..	Azz[(XX%	""E#56
	  ~~a+(ME~e,LL'##**626==)6;;66 ) [)@)@(AATUVVr   c                    U R                   (       a  SU l         g U R                  5       u  U l        U l        U R                  c   S5       eU R                  c   S5       e[	        U R
                  U R                  5      nU" U R                  U R                  [        X R                  5      /U R                  Q70 U R                  D6U l        g )Nr   zMust have a host to connect to.zMust have a port to connect to.)stopAfterDNSr   hostr~   r&   r8   r>   r   r<   r?   r@   r   )r   connectFuncs     r   rJ   SRVConnector._reallyConnect   s     !D#0	49yy$G&GG$yy$G&GG$dllD,@,@A$IIII.t\\B
 !!	

 $$
r   c                 j    U R                   (       a  U R                   R                  5         gSU l        g)zStop attempting to connect.r`   N)r   stopConnectingr   r   s    r   r   SRVConnector.stopConnecting   s!    >>NN))+ !Dr   c                 t    U R                   b  U R                   R                  5         gU R                  5         g)z%Disconnect whatever our are state is.N)r   
disconnectr   r   s    r   r   SRVConnector.disconnect   s(    >>%NN%%'!r   c                 \    U R                   (       d   eU R                   R                  5       $ r   )r   getDestinationr   s    r   r   SRVConnector.getDestination  s!    ~~~~~,,..r   c                 n    U R                   R                  X5        U R                   R                  5         g r   )r<   r   doStopr   r   s     r   r   SRVConnector.connectionFailed	  s$    ++D9r   c                 n    U R                   R                  X5        U R                   R                  5         g r   )r<   r#   r   r   s     r   r"   SRVConnector.connectionLost  s$    ))$7r   )rA   r?   r@   r>   r   r;   r<   r   rC   r~   r=   r8   rB   r9   r   )r,   r-   r.   r/   __doc__r   r   rW   rS   rR   rV   r   rJ   r   r   r   r   r"   r0   r1   r   r   r3   r3   "   s^      L $%#N%:!2,;Wz
$""/r   r3   )r{   zope.interfacer   twisted.internetr   r   twisted.namesr   r   twisted.names.errorr   twisted.python.compatr	   r   
IConnectorr3   r1   r   r   <module>r      sJ     & . % , .3 3$ Z""#l l $lr   