zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
U
Z±dEã@s†ddlZddlmZddlZddlmZddlmZmZm    Z    ej
dkrRddl m Z dZ edd    „ƒZd
d „Zd d „Ze ddœdd„ZdS)éN)Úcontextmanager)Ú
MultiError)Ú getaddrinfoÚ SOCK_STREAMÚsocket)éé )ÚExceptionGroupgÐ?ccs‚tƒ}z
|VW5g}|D]>}z | ¡Wqtk
rV}z| |¡W5d}~XYqXqt|ƒdkrp|d‚n |r|t|ƒ‚XdS©Nér)ÚsetÚcloseÚ BaseExceptionÚappendÚlenr)Zsockets_to_closeZerrsÚsockÚexc©rúVd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\trio/_highlevel_open_tcp_stream.pyÚ    close_allos
 
rcCsNtdt|ƒƒD]:}||d|ddkr|dkrD| d| |¡¡qJqdSr
)ÚrangerÚinsertÚpop)ÚtargetsÚirrrÚ reorder_for_rfc_6555_section_5_4s
    rcCs<t|tƒr| d¡n|}d|kr,d ||¡Sd ||¡SdS)NÚasciiú:z[{}]:{}z{}:{})Ú
isinstanceÚbytesÚdecodeÚformat)ÚhostÚportrrrÚformat_host_port“s r$)Úhappy_eyeballs_delayÚ local_addressc ƒst|dkrtdƒ‚t|tƒs(td |¡ƒ‚|dkr4t}t||tdIdH}|sdd t||ƒ¡}t    |ƒ‚t
|ƒg‰d‰‡‡‡‡‡fdd„}t ƒÜ‰t   ¡4IdHšT‰|D]H^}}}    t  ¡}
ˆ |||    |
¡t  |¡|
 ¡IdHW5QRXq¦W5QIdHRXˆdkrBtˆƒt|ƒks t‚d t||ƒ¡}t    |ƒt|ˆƒ‚n$t  ˆ¡} ˆ ˆ¡| W5QR£SW5QRXdS)    a¿ Connect to the given host and port over TCP.
 
    If the given ``host`` has multiple IP addresses associated with it, then
    we have a problem: which one do we use?
 
    One approach would be to attempt to connect to the first one, and then if
    that fails, attempt to connect to the second one ... until we've tried all
    of them. But the problem with this is that if the first IP address is
    unreachable (for example, because it's an IPv6 address and our network
    discards IPv6 packets), then we might end up waiting tens of seconds for
    the first connection attempt to timeout before we try the second address.
 
    Another approach would be to attempt to connect to all of the addresses at
    the same time, in parallel, and then use whichever connection succeeds
    first, abandoning the others. This would be fast, but create a lot of
    unnecessary load on the network and the remote server.
 
    This function strikes a balance between these two extremes: it works its
    way through the available addresses one at a time, like the first
    approach; but, if ``happy_eyeballs_delay`` seconds have passed and it's
    still waiting for an attempt to succeed or fail, then it gets impatient
    and starts the next connection attempt in parallel. As soon as any one
    connection attempt succeeds, all the other attempts are cancelled. This
    avoids unnecessary load because most connections will succeed after just
    one or two attempts, but if one of the addresses is unreachable then it
    doesn't slow us down too much.
 
    This is known as a "happy eyeballs" algorithm, and our particular variant
    is modelled after how Chrome connects to webservers; see `RFC 6555
    <https://tools.ietf.org/html/rfc6555>`__ for more details.
 
    Args:
      host (str or bytes): The host to connect to. Can be an IPv4 address,
          IPv6 address, or a hostname.
 
      port (int): The port to connect to.
 
      happy_eyeballs_delay (float): How many seconds to wait for each
          connection attempt to succeed or fail before getting impatient and
          starting another one in parallel. Set to `math.inf` if you want
          to limit to only one connection attempt at a time (like
          :func:`socket.create_connection`). Default: 0.25 (250 ms).
 
      local_address (None or str): The local IP address or hostname to use as
          the source for outgoing connections. If ``None``, we let the OS pick
          the source IP.
 
          This is useful in some exotic networking configurations where your
          host has multiple IP addresses, and you want to force the use of a
          specific one.
 
          Note that if you pass an IPv4 ``local_address``, then you won't be
          able to connect to IPv6 hosts, and vice-versa. If you want to take
          advantage of this to force the use of IPv4 or IPv6 without
          specifying an exact source address, you can use the IPv4 wildcard
          address ``local_address="0.0.0.0"``, or the IPv6 wildcard address
          ``local_address="::"``.
 
    Returns:
      SocketStream: a :class:`~trio.abc.Stream` connected to the given server.
 
    Raises:
      OSError: if the connection fails.
 
    See also:
      open_ssl_over_tcp_stream
 
    Nzhost cannot be Nonezport must be int, not {!r})Útypez(no results found for hostname lookup: {}c
“sìz²t|Ž}ˆ |¡ˆdk    r’z| tjjtjjd¡Wnttfk
rNYnXz| ˆdf¡IdHWn(tk
rtdˆ›d|›ƒ‚YnX|     |¡IdH|‰ˆj
  ¡Wn4tk
ræ}zˆ  |¡|  ¡W5d}~XYnXdS)Nr rzlocal_address=z% is incompatible with remote address )rÚaddÚ
setsockoptÚtrioÚ
IPPROTO_IPZIP_BIND_ADDRESS_NO_PORTÚOSErrorÚAttributeErrorÚbindÚconnectZ cancel_scopeÚcancelrr )Z socket_argsZsockaddrÚattempt_failedrr©r&ZnurseryZ open_socketsZoserrorsZwinning_socketrrÚattempt_connects0
!ÿÿ
 
z(open_tcp_stream.<locals>.attempt_connectz$all attempts to connect to {} failed)Ú
ValueErrorrÚintÚ    TypeErrorr!Ú DEFAULT_DELAYrrr$r,rrr*Z open_nurseryÚEventZ
start_soonZ move_on_afterÚwaitrÚAssertionErrorr    Z SocketStreamÚremove) r"r#r%r&rÚmsgr3ÚsaÚ_Úaddrr1Ústreamrr2rÚopen_tcp_stream¯s@L
ÿ    D *
ÿ
 
rA)ÚsysÚ
contextlibrr*Ztrio._core._multierrorrZ trio.socketrrrÚ version_infoZexceptiongroupr    r7rrr$rArrrrÚ<module>s  
 <*
ÿ