Format: application/x-python-code
⬇ Download File (21.9 KB)
   1  o
   2  
   3  ;'hj�@s4ddlmZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
   4  mZmZddlm
   5  Z
   6  mZddlmZmZddlmZmZmZmZmZdd	lmZdd
   7  lmZmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&d
   8  dl'm(Z(d
   9  dl)m*Z*d
  10  dl+m,Z,d
  11  dl-m.Z.m/Z/er�ddl0m1Z1ne2Z1ej3dkr�ddl4m5Z5ej3dkr�ddl6m7Z7nddl8m7Z7e9edd�Z:eej;ej<ej=fZ>eej<ej=fZ?eddddd�d�d+d,��Z@eddddd-�d�d0d,��Z@edddddd1�d�d4d,��Z@edddddd1�d�d7d,��Z@eddd8�d�d9d,��Z@dd:dd;dd<d=�d�d?d,�Z@d�dCdD�ZAddejj;dEd:dF�d�dMdN�ZBddEdO�d�dSdT�ZCej;fddd:dU�d�dWdX�ZDej;ddd:dY�d�d[d\�ZEddd]�d�dbdc�ZFddd]�d�dfdg�ZGdddddh�d�dsdt�ZHd�d�dxdy�ZIe7dz�d�d~d��ZJe7d��d�d�d���ZKd�d�d��ZLd�d�d��ZMd�d�d��ZNd�d�d��ZOdS)��)�annotationsN)�	Awaitable)�IPv6Address�
  12  ip_address)�PathLike�chmod)�
  13  AddressFamily�
  14  SocketKind)�
  15  TYPE_CHECKING�Any�Literal�cast�overload�)�	to_thread)	�ConnectedUDPSocket�ConnectedUNIXDatagramSocket�
  16  IPAddressType�IPSockAddrType�SocketListener�SocketStream�	UDPSocket�UNIXDatagramSocket�UNIXSocketStream)�
  17  MultiListener)�	TLSStream�)�get_async_backend)�aclose_forcefully)�Event)�create_task_group�
  18  move_on_after)�FileDescriptorLike)��)�ExceptionGroup)r#�
  19  )�
  20  deprecated�IPPROTO_IPV6�).)�
  21  local_host�ssl_context�tls_standard_compatible�happy_eyeballs_delay�remote_hostr�remote_port�intr*�IPAddressType | Noner+�ssl.SSLContext | Noner,�bool�tls_hostname�strr-�float�returnrc���dS�N��r.r/r*r+r,r4r-r:r:�JC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/_core/_sockets.py�connect_tcp8��
  22  r=)r*r,r4r-�ssl.SSLContext�
  23  str | Nonec�r8r9r:r;r:r:r<r=Fr>)r*r+r,r4r-�tls�
  24  Literal[True]c�r8r9r:�r.r/r*rAr+r,r4r-r:r:r<r=T���Literal[False]rc�r8r9r:rCr:r:r<r=crD)r*r-c�r8r9r:)r.r/r*r-r:r:r<r=rs�FTg�?)r*rAr+r,r4r-�SocketStream | TLSStreamc
  25  �s��d�d������fdd	�}t��d�tj}	|r+tt|�d�IdH}
  26  |
  27  d
  28  �^}	}�t|�}zt|�}
  29  Wnty@d}
  30  Ynw|
  31  durZt|
  32  t�rRtj	|
  33  j
  34  fg}nVtj|
  35  j
  36  fg}nNt|�|	tjd�IdH}
  37  d}}g}|
  38  D]9�^}}}|tj	kr�|s�d
  39  }|�
  40  d
  41  ||d
  42  f�qn|tjkr�|s�|r�d
  43  }|�
  44  d||d
  45  f�qn|�||d
  46  f�qng�zmt�4IdH�;�t|�D]+\}\}}t�}��|||�t|��|��IdHWd�n1s�wYq�Wd�IdHn1IdHs�wY�du�rt��dk�r�d
  47  ntd��}td�|�W���n���w|�s&|�s&|�rJztj�d|�p1t|�||d�IdHWSt�yIt��IdH�w�S)a~
  48      Connect to a host using the TCP protocol.
  49  
  50      This function implements the stateless version of the Happy Eyeballs algorithm (RFC
  51      6555). If ``remote_host`` is a host name that resolves to multiple IP addresses,
  52      each one is tried until one connection attempt succeeds. If the first attempt does
  53      not connected within 250 milliseconds, a second attempt is started using the next
  54      address in the list, and so on. On IPv6 enabled systems, an IPv6 address (if
  55      available) is tried first.
  56  
  57      When the connection has been established, a TLS handshake will be done if either
  58      ``ssl_context`` or ``tls_hostname`` is not ``None``, or if ``tls`` is ``True``.
  59  
  60      :param remote_host: the IP address or host name to connect to
  61      :param remote_port: port on the target host to connect to
  62      :param local_host: the interface address or name to bind the socket to before
  63          connecting
  64      :param tls: ``True`` to do a TLS handshake with the connected stream and return a
  65          :class:`~anyio.streams.tls.TLSStream` instead
  66      :param ssl_context: the SSL context object to use (if omitted, a default context is
  67          created)
  68      :param tls_standard_compatible: If ``True``, performs the TLS shutdown handshake
  69          before closing the stream and requires that the server does this as well.
  70          Otherwise, :exc:`~ssl.SSLEOFError` may be raised during reads from the stream.
  71          Some protocols, such as HTTP, require this option to be ``False``.
  72          See :meth:`~ssl.SSLContext.wrap_socket` for details.
  73      :param tls_hostname: host name to check the server certificate against (defaults to
  74          the value of ``remote_host``)
  75      :param happy_eyeballs_delay: delay (in seconds) before starting the next connection
  76          attempt
  77      :return: a socket stream object if no TLS handshake was done, otherwise a TLS stream
  78      :raises OSError: if the connection attempt fails
  79  
  80      Nr.r5�eventrr7�Nonec
  81  �s��zKz��|���IdH}Wnty+}z��|�WYd}~W|��dSd}~ww�dur8|��j��n|��IdHW|��dSW|��dS|��wr9)r=�OSError�append�setZcancel_scope�cancel�aclose)r.rG�stream�exc��asynclibZconnected_stream�
  82  local_addressZoserrorsr/�tgr:r<�try_connect�s �
  83  ���z connect_tcp.<locals>.try_connectr��family�typeFTrz#multiple connection attempts failedzAll connection attempts failed)�server_side�hostnamer+Zstandard_compatible)r.r5rGrr7rH)r�socket�	AF_UNSPEC�getaddrinfor5r�
  84  ValueError�
  85  isinstancer�AF_INET6�
  86  compressed�AF_INET�SOCK_STREAM�insertrJr �	enumeraterZ
  87  start_soonr!�wait�lenr%rI�clearr�wrap�
  88  BaseExceptionr)r.r/r*rAr+r,r4r-rTrV�gai_res�_Ztarget_hostZaddr_objZtarget_addrsZv6_foundZv4_found�af�rest�sa�i�addrrG�causer:rPr<r=|s��.�
  89  
  90  �
  91  ���(�
  92  ��
  93  ����path�str | bytes | PathLike[Any]rc�s�t�|�}t��|�IdHS)z�
  94      Connect to the given UNIX socket.
  95  
  96      Not available on Windows.
  97  
  98      :param path: path to the socket
  99      :return: a socket stream object
 100  
 101      N)�os�fspathr�connect_unix)rrr:r:r<rvs�
 102  
 103  rv�)r*�
 104  local_portrV�backlog�
 105  reuse_portrxrV�AnyIPAddressFamilyryrz�MultiListener[SocketStream]c�s��t�}t|d�}|durt|�nd}t|||tjdkr tjjndtj	tj
 106  Bd�IdH}g}z�tt|��D]z�^}}	}
 107  }tjdkrG|	tjurGq5t�|�}|�
 108  d�tjdkr`|�tjtjd�n	|�tjtjd�|rt|�tjtjd�|tjkr�|�ttjd�d|dvr�|d�dd�\}
 109  }|
 110  |ddt|�f}|�|�|�|�|�|�}|�|�q5Wt|�Sty�|D]	}|��IdHq��w)	a�
 111      Create a TCP socket listener.
 112  
 113      :param local_port: port number to listen on
 114      :param local_host: IP address of the interface to listen on. If omitted, listen on
 115          all IPv4 and IPv6 interfaces. To listen on all interfaces on a specific address
 116          family, use ``0.0.0.0`` for IPv4 or ``::`` for IPv6.
 117      :param family: address family (used if ``local_host`` was omitted)
 118      :param backlog: maximum number of queued incoming connections (up to a maximum of
 119          2**16, or 65536)
 120      :param reuse_port: ``True`` to allow multiple sockets to bind to the same
 121          address/port (not supported on Windows)
 122      :return: a list of listener objects
 123  
 124      rwN�win32r�rVrW�flagsFr�%)r�minr5r\�sys�platformrZr	rb�
 125  AI_PASSIVE�
 126  AI_ADDRCONFIG�sortedrK�setblocking�
 127  setsockopt�
 128  SOL_SOCKET�SO_EXCLUSIVEADDRUSE�SO_REUSEADDR�SO_REUSEPORTr_r(�IPV6_V6ONLY�splitr0�bind�listen�create_tcp_listenerrJrirMr)r*rxrVryrzrQrjZ	listeners�fam�kindrk�sockaddr�
 129  raw_socketrp�scope_id�listenerr:r:r<r�sN�
 130  
 131  �
 132  
 133  
 134  
 135  
 136  
 137  
 138  �'��r�)�moderyr��
 139  int | Nonerc�sR�t|d�}t||tj�IdH}z|�|�t��|�WSty(|���w)a�
 140      Create a UNIX socket listener.
 141  
 142      Not available on Windows.
 143  
 144      :param path: path of the socket
 145      :param mode: permissions to set on the socket
 146      :param backlog: maximum number of queued incoming connections (up to a maximum of
 147          2**16, or 65536)
 148      :return: a listener object
 149  
 150      .. versionchanged:: 3.0
 151          If a socket already exists on the file system in the given path, it will be
 152          removed first.
 153  
 154      rwN)	r��setup_unix_local_socketrZrbr�r�create_unix_listenerri�close)rrr�ryr�r:r:r<r�cs�
 155  
 156  �r�)r*rxrzrc�s��|tjur|std��|r1tt|�||tjtjtjBd�IdH}t	t
 157  |dd�}|dd}n
 158  |tjur9d}nd}t��
 159  ||d|�IdH}t	t|�S)uY
 160      Create a UDP socket.
 161  
 162      If ``port`` has been given, the socket will be bound to this port on the local
 163      machine, making this socket suitable for providing UDP based services.
 164  
 165      :param family: address family (``AF_INET`` or ``AF_INET6``) – automatically
 166          determined from ``local_host`` if omitted
 167      :param local_host: IP address or host name of the local interface to bind to
 168      :param local_port: local port to bind to
 169      :param reuse_port: ``True`` to allow multiple sockets to bind to the same
 170          address/port (not supported on Windows)
 171      :return: a UDP socket
 172  
 173      z-Either "family" or "local_host" must be givenr~Nr�����)z::r)z0.0.0.0r)rr[r]r\r5rZ�
 174  SOCK_DGRAMr�r�r
 175  r{r_r�create_udp_socketr)rVr*rxrzrjrR�sockr:r:r<r��s(�
 176  �
 177  
 178  �
 179  r�)rVr*rxrzrc
 180  �s��d}|r'tt|�||tjtjtjBd�IdH}tt|dd�}|dd}tt|�||tjd�IdH}tt|dd�}|dd}t��	||||�IdH}	tt
 181  |	�S)u�
 182      Create a connected UDP socket.
 183  
 184      Connected UDP sockets can only communicate with the specified remote host/port, an
 185      any packets sent from other sources are dropped.
 186  
 187      :param remote_host: remote host to set as the default target
 188      :param remote_port: port on the remote host to set as the default target
 189      :param family: address family (``AF_INET`` or ``AF_INET6``) – automatically
 190          determined from ``local_host`` or ``remote_host`` if omitted
 191      :param local_host: IP address or host name of the local interface to bind to
 192      :param local_port: local port to bind to
 193      :param reuse_port: ``True`` to allow multiple sockets to bind to the same
 194          address/port (not supported on Windows)
 195      :return: a connected UDP socket
 196  
 197      Nr~rr�rU)r\r5rZr�r�r�r
 198  r{rr�r)
 199  r.r/rVr*rxrzrRrj�remote_addressr�r:r:r<�create_connected_udp_socket�s*�
 200  ��
 201  �
 202  r�)�
 203  local_path�
 204  local_moder��"None | str | bytes | PathLike[Any]r�rc�s*�t||tj�IdH}t��|d�IdHS)a8
 205      Create a UNIX datagram socket.
 206  
 207      Not available on Windows.
 208  
 209      If ``local_path`` has been given, the socket will be bound to this path, making this
 210      socket suitable for receiving datagrams from other processes. Other processes can
 211      send datagrams to this socket only if ``local_path`` is set.
 212  
 213      If a socket already exists on the file system in the ``local_path``, it will be
 214      removed first.
 215  
 216      :param local_path: the path on which to bind to
 217      :param local_mode: permissions to set on the local socket
 218      :return: a UNIX datagram socket
 219  
 220      N)r�rZr�r�create_unix_datagram_socket)r�r�r�r:r:r<r��s
 221  �
 222  �r��remote_pathrc�s4�t�|�}t||tj�IdH}t��||�IdHS)a�
 223      Create a connected UNIX datagram socket.
 224  
 225      Connected datagram sockets can only communicate with the specified remote path.
 226  
 227      If ``local_path`` has been given, the socket will be bound to this path, making
 228      this socket suitable for receiving datagrams from other processes. Other processes
 229      can send datagrams to this socket only if ``local_path`` is set.
 230  
 231      If a socket already exists on the file system in the ``local_path``, it will be
 232      removed first.
 233  
 234      :param remote_path: the path to set as the default target
 235      :param local_path: the path on which to bind to
 236      :param local_mode: permissions to set on the local socket
 237      :return: a connected UNIX datagram socket
 238  
 239      N)rtrur�rZr�rr�)r�r�r�r�r:r:r<�%create_connected_unix_datagram_socket�s�
 240  
 241  �
 242  �r��rVrW�protor�host�bytes | str | None�port�str | int | None�int | AddressFamilyrW�int | SocketKindr�r�Alist[tuple[AddressFamily, SocketKind, int, str, tuple[str, int]]]c	�st�t|t�r"z|�d�}Wnty!ddl}|j|dd�}Ynw|}t�j||||||d�IdH}dd�|D�S)	a�
 243      Look up a numeric IP address given a host name.
 244  
 245      Internationalized domain names are translated according to the (non-transitional)
 246      IDNA 2008 standard.
 247  
 248      .. note:: 4-tuple IPv6 socket addresses are automatically converted to 2-tuples of
 249          (host, port), unlike what :func:`socket.getaddrinfo` does.
 250  
 251      :param host: host name
 252      :param port: port number
 253      :param family: socket family (`'AF_INET``, ...)
 254      :param type: socket type (``SOCK_STREAM``, ...)
 255      :param proto: protocol number
 256      :param flags: flags to pass to upstream ``getaddrinfo()``
 257      :return: list of tuples containing (family, type, proto, canonname, sockaddr)
 258  
 259      .. seealso:: :func:`socket.getaddrinfo`
 260  
 261      �asciirNT)�uts46r�cSs6g|]\}}}}}t|dt�s||||t|�f�qS�r)r^r0�convert_ipv6_sockaddr)�.0rVrWr��	canonnamer�r:r:r<�
 262  <listcomp>Ks��zgetaddrinfo.<locals>.<listcomp>)r^r5�encode�UnicodeEncodeError�idnarr\)	r�r�rVrWr�rZencoded_hostr�rjr:r:r<r\ s�
 263  ���r\r�r�Awaitable[tuple[str, str]]cCst��||�S)a
 264      Look up the host name of an IP address.
 265  
 266      :param sockaddr: socket address (e.g. (ipaddress, port) for IPv4)
 267      :param flags: flags to pass to upstream ``getnameinfo()``
 268      :return: a tuple of (host name, service name)
 269  
 270      .. seealso:: :func:`socket.getnameinfo`
 271  
 272      )r�getnameinfo)r�rr:r:r<r�Ssr�z8This function is deprecated; use `wait_readable` insteadr��
 273  socket.socket�Awaitable[None]cC�t��|���S)a
 274      .. deprecated:: 4.7.0
 275         Use :func:`wait_readable` instead.
 276  
 277      Wait until the given socket has data to be read.
 278  
 279      .. warning:: Only use this on raw sockets that have not been wrapped by any higher
 280          level constructs like socket streams!
 281  
 282      :param sock: a socket object
 283      :raises ~anyio.ClosedResourceError: if the socket was closed while waiting for the
 284          socket to become readable
 285      :raises ~anyio.BusyResourceError: if another task is already waiting for the socket
 286          to become readable
 287  
 288      )r�
 289  wait_readable�fileno�r�r:r:r<�wait_socket_readableasr�z8This function is deprecated; use `wait_writable` insteadcCr�)a�
 290      .. deprecated:: 4.7.0
 291         Use :func:`wait_writable` instead.
 292  
 293      Wait until the given socket can be written to.
 294  
 295      This does **NOT** work on Windows when using the asyncio backend with a proactor
 296      event loop (default on py3.8+).
 297  
 298      .. warning:: Only use this on raw sockets that have not been wrapped by any higher
 299          level constructs like socket streams!
 300  
 301      :param sock: a socket object
 302      :raises ~anyio.ClosedResourceError: if the socket was closed while waiting for the
 303          socket to become writable
 304      :raises ~anyio.BusyResourceError: if another task is already waiting for the socket
 305          to become writable
 306  
 307      )r�
 308  wait_writabler�r�r:r:r<�wait_socket_writablevsr��objr"cC�t��|�S)aJ
 309      Wait until the given object has data to be read.
 310  
 311      On Unix systems, ``obj`` must either be an integer file descriptor, or else an
 312      object with a ``.fileno()`` method which returns an integer file descriptor. Any
 313      kind of file descriptor can be passed, though the exact semantics will depend on
 314      your kernel. For example, this probably won't do anything useful for on-disk files.
 315  
 316      On Windows systems, ``obj`` must either be an integer ``SOCKET`` handle, or else an
 317      object with a ``.fileno()`` method which returns an integer ``SOCKET`` handle. File
 318      descriptors aren't supported, and neither are handles that refer to anything besides
 319      a ``SOCKET``.
 320  
 321      On backends where this functionality is not natively provided (asyncio
 322      ``ProactorEventLoop`` on Windows), it is provided using a separate selector thread
 323      which is set to shut down when the interpreter shuts down.
 324  
 325      .. warning:: Don't use this on raw sockets that have been wrapped by any higher
 326          level constructs like socket streams!
 327  
 328      :param obj: an object with a ``.fileno()`` method or an integer handle
 329      :raises ~anyio.ClosedResourceError: if the object was closed while waiting for the
 330          object to become readable
 331      :raises ~anyio.BusyResourceError: if another task is already waiting for the object
 332          to become readable
 333  
 334      )rr��r�r:r:r<r��sr�cCr�)a~
 335      Wait until the given object can be written to.
 336  
 337      :param obj: an object with a ``.fileno()`` method or an integer handle
 338      :raises ~anyio.ClosedResourceError: if the object was closed while waiting for the
 339          object to become writable
 340      :raises ~anyio.BusyResourceError: if another task is already waiting for the object
 341          to become writable
 342  
 343      .. seealso:: See the documentation of :func:`wait_readable` for the definition of
 344         ``obj`` and notes on backend compatibility.
 345  
 346      .. warning:: Don't use this on raw sockets that have been wrapped by any higher
 347          level constructs like socket streams!
 348  
 349      )rr�r�r:r:r<r��sr��+tuple[str, int, int, int] | tuple[str, int]�tuple[str, int]cCsRt|t�r't|�dkr'|\}}}}|r#|�d�d}|�d|��|fS||fS|S)a�
 350      Convert a 4-tuple IPv6 socket address to a 2-tuple (address, port) format.
 351  
 352      If the scope ID is nonzero, it is added to the address, separated with ``%``.
 353      Otherwise the flow id and scope id are simply cut off from the tuple.
 354      Any other kinds of socket addresses are returned as-is.
 355  
 356      :param sockaddr: the result of :meth:`~socket.socket.getsockname`
 357      :return: the converted socket address
 358  
 359      �r�r)r^�tuplerfr�)r�r�r��flowinfor�r:r:r<r��sr��socktypec
 360  �s�|durCt�|�}|�d�sBzt�|�}Wn ty6}z|jtjtjtjtj	fvr,�WYd}~nd}~wwt�
 361  |j�rBt�|�nd}t
 362  �
 363  t
 364  j|�}|�d�|dur�z"tj|j|dd�IdH|durutjt||dd�IdHW|SW|Sty�|���w|S)a
 365      Create a UNIX local socket object, deleting the socket at the given path if it
 366      exists.
 367  
 368      Not available on Windows.
 369  
 370      :param path: path of the socket
 371      :param mode: permissions to set on the socket
 372      :param socktype: socket.SOCK_STREAM or socket.SOCK_DGRAM
 373  
 374      N�FT)Zabandon_on_cancel)rt�fsdecode�
 375  startswith�statrI�errno�ENOENT�ENOTDIR�EBADF�ELOOP�S_ISSOCK�st_mode�unlinkrZ�AF_UNIXr�rZrun_syncr�rrir�)rrr�r�Zpath_str�stat_result�er�r:r:r<r��sH�
 376  
 377  ����	
 378  �
 379  ���r�)r.rr/r0r*r1r+r2r,r3r4r5r-r6r7r)r.rr/r0r*r1r+r?r,r3r4r@r-r6r7r)r.rr/r0r*r1rArBr+r2r,r3r4r@r-r6r7r)r.rr/r0r*r1rArEr+r2r,r3r4r@r-r6r7r)
 380  r.rr/r0r*r1r-r6r7r)r.rr/r0r*r1rAr3r+r2r,r3r4r@r-r6r7rF)rrrsr7r)r*r1rxr0rVr{ryr0rzr3r7r|)rrrsr�r�ryr0r7r)
 381  rVr{r*r1rxr0rzr3r7r)r.rr/r0rVr{r*r1rxr0rzr3r7r)r�r�r�r�r7r)r�rsr�r�r�r�r7r)r�r�r�r�rVr�rWr�r�r0rr0r7r�r�)r�rrr0r7r�)r�r�r7r�)r�r"r7r�)r�r�r7r�)rrr�r�r�r�r0r7r�)P�
 382  __future__rr�rtrZ�sslr�r��collections.abcr�	ipaddressrrrrrr	�typingr
 383  rrr
 384  r�r�abcrrrrrrrrrZstreams.stapledrZstreams.tlsrZ
 385  _eventlooprZ
 386  _resourcesrZ_synchronizationr�_tasksr r!Z	_typeshedr"�object�version_infoZexceptiongroupr%Ztyping_extensionsr'�warnings�getattrr(r[rar_r{ZIPAddressFamilyr=rvr�r�r�r�r�r�r\r�r�r�r�r�r�r�r:r:r:r<�<module>s�,
 387  
 388  ��
 389  �
 390  ���
 391  �
 392  
 393  �S�!��2�4��%�3
 394  
 395  
 396