client.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. from __future__ import annotations
  2. import asyncio
  3. import logging
  4. import os
  5. import socket
  6. import ssl as ssl_module
  7. import traceback
  8. import urllib.parse
  9. from collections.abc import AsyncIterator, Generator, Sequence
  10. from types import TracebackType
  11. from typing import Any, Callable, Literal, cast
  12. from ..client import ClientProtocol, backoff
  13. from ..datastructures import Headers, HeadersLike
  14. from ..exceptions import (
  15. InvalidMessage,
  16. InvalidProxyMessage,
  17. InvalidProxyStatus,
  18. InvalidStatus,
  19. ProxyError,
  20. SecurityError,
  21. )
  22. from ..extensions.base import ClientExtensionFactory
  23. from ..extensions.permessage_deflate import enable_client_permessage_deflate
  24. from ..headers import validate_subprotocols
  25. from ..http11 import USER_AGENT, Response
  26. from ..protocol import CONNECTING, Event
  27. from ..proxy import Proxy, get_proxy, parse_proxy, prepare_connect_request
  28. from ..streams import StreamReader
  29. from ..typing import LoggerLike, Origin, Subprotocol
  30. from ..uri import WebSocketURI, parse_uri
  31. from .connection import Connection
  32. __all__ = ["connect", "unix_connect", "ClientConnection"]
  33. MAX_REDIRECTS = int(os.environ.get("WEBSOCKETS_MAX_REDIRECTS", "10"))
  34. class ClientConnection(Connection):
  35. """
  36. :mod:`asyncio` implementation of a WebSocket client connection.
  37. :class:`ClientConnection` provides :meth:`recv` and :meth:`send` coroutines
  38. for receiving and sending messages.
  39. It supports asynchronous iteration to receive messages::
  40. async for message in websocket:
  41. await process(message)
  42. The iterator exits normally when the connection is closed with code
  43. 1000 (OK) or 1001 (going away) or without a close code. It raises a
  44. :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
  45. closed with any other code.
  46. The ``ping_interval``, ``ping_timeout``, ``close_timeout``, ``max_queue``,
  47. and ``write_limit`` arguments have the same meaning as in :func:`connect`.
  48. Args:
  49. protocol: Sans-I/O connection.
  50. """
  51. def __init__(
  52. self,
  53. protocol: ClientProtocol,
  54. *,
  55. ping_interval: float | None = 20,
  56. ping_timeout: float | None = 20,
  57. close_timeout: float | None = 10,
  58. max_queue: int | None | tuple[int | None, int | None] = 16,
  59. write_limit: int | tuple[int, int | None] = 2**15,
  60. ) -> None:
  61. self.protocol: ClientProtocol
  62. super().__init__(
  63. protocol,
  64. ping_interval=ping_interval,
  65. ping_timeout=ping_timeout,
  66. close_timeout=close_timeout,
  67. max_queue=max_queue,
  68. write_limit=write_limit,
  69. )
  70. self.response_rcvd: asyncio.Future[None] = self.loop.create_future()
  71. async def handshake(
  72. self,
  73. additional_headers: HeadersLike | None = None,
  74. user_agent_header: str | None = USER_AGENT,
  75. ) -> None:
  76. """
  77. Perform the opening handshake.
  78. """
  79. self.request = self.protocol.connect()
  80. if additional_headers is not None:
  81. self.request.headers.update(additional_headers)
  82. if user_agent_header is not None:
  83. self.request.headers.setdefault("User-Agent", user_agent_header)
  84. async with self.send_context(expected_state=CONNECTING):
  85. self.protocol.send_request(self.request)
  86. await asyncio.wait(
  87. [self.response_rcvd, self.connection_lost_waiter],
  88. return_when=asyncio.FIRST_COMPLETED,
  89. )
  90. # self.protocol.handshake_exc is set when the connection is lost before
  91. # receiving a response, when the response cannot be parsed, or when the
  92. # response fails the handshake.
  93. if self.protocol.handshake_exc is not None:
  94. raise self.protocol.handshake_exc
  95. def process_event(self, event: Event) -> None:
  96. """
  97. Process one incoming event.
  98. """
  99. # First event - handshake response.
  100. if self.response is None:
  101. assert isinstance(event, Response)
  102. self.response = event
  103. self.response_rcvd.set_result(None)
  104. # Later events - frames.
  105. else:
  106. super().process_event(event)
  107. def process_exception(exc: Exception) -> Exception | None:
  108. """
  109. Determine whether a connection error is retryable or fatal.
  110. When reconnecting automatically with ``async for ... in connect(...)``, if a
  111. connection attempt fails, :func:`process_exception` is called to determine
  112. whether to retry connecting or to raise the exception.
  113. This function defines the default behavior, which is to retry on:
  114. * :exc:`EOFError`, :exc:`OSError`, :exc:`asyncio.TimeoutError`: network
  115. errors;
  116. * :exc:`~websockets.exceptions.InvalidStatus` when the status code is 500,
  117. 502, 503, or 504: server or proxy errors.
  118. All other exceptions are considered fatal.
  119. You can change this behavior with the ``process_exception`` argument of
  120. :func:`connect`.
  121. Return :obj:`None` if the exception is retryable i.e. when the error could
  122. be transient and trying to reconnect with the same parameters could succeed.
  123. The exception will be logged at the ``INFO`` level.
  124. Return an exception, either ``exc`` or a new exception, if the exception is
  125. fatal i.e. when trying to reconnect will most likely produce the same error.
  126. That exception will be raised, breaking out of the retry loop.
  127. """
  128. # This catches python-socks' ProxyConnectionError and ProxyTimeoutError.
  129. if isinstance(exc, (OSError, TimeoutError)):
  130. return None
  131. if isinstance(exc, InvalidMessage) and isinstance(exc.__cause__, EOFError):
  132. return None
  133. if isinstance(exc, InvalidStatus) and exc.response.status_code in [
  134. 500, # Internal Server Error
  135. 502, # Bad Gateway
  136. 503, # Service Unavailable
  137. 504, # Gateway Timeout
  138. ]:
  139. return None
  140. return exc
  141. # This is spelled in lower case because it's exposed as a callable in the API.
  142. class connect:
  143. """
  144. Connect to the WebSocket server at ``uri``.
  145. This coroutine returns a :class:`ClientConnection` instance, which you can
  146. use to send and receive messages.
  147. :func:`connect` may be used as an asynchronous context manager::
  148. from websockets.asyncio.client import connect
  149. async with connect(...) as websocket:
  150. ...
  151. The connection is closed automatically when exiting the context.
  152. :func:`connect` can be used as an infinite asynchronous iterator to
  153. reconnect automatically on errors::
  154. async for websocket in connect(...):
  155. try:
  156. ...
  157. except websockets.exceptions.ConnectionClosed:
  158. continue
  159. If the connection fails with a transient error, it is retried with
  160. exponential backoff. If it fails with a fatal error, the exception is
  161. raised, breaking out of the loop.
  162. The connection is closed automatically after each iteration of the loop.
  163. Args:
  164. uri: URI of the WebSocket server.
  165. origin: Value of the ``Origin`` header, for servers that require it.
  166. extensions: List of supported extensions, in order in which they
  167. should be negotiated and run.
  168. subprotocols: List of supported subprotocols, in order of decreasing
  169. preference.
  170. compression: The "permessage-deflate" extension is enabled by default.
  171. Set ``compression`` to :obj:`None` to disable it. See the
  172. :doc:`compression guide <../../topics/compression>` for details.
  173. additional_headers: Arbitrary HTTP headers to add to the handshake
  174. request.
  175. user_agent_header: Value of the ``User-Agent`` request header.
  176. It defaults to ``"Python/x.y.z websockets/X.Y"``.
  177. Setting it to :obj:`None` removes the header.
  178. proxy: If a proxy is configured, it is used by default. Set ``proxy``
  179. to :obj:`None` to disable the proxy or to the address of a proxy
  180. to override the system configuration. See the :doc:`proxy docs
  181. <../../topics/proxies>` for details.
  182. process_exception: When reconnecting automatically, tell whether an
  183. error is transient or fatal. The default behavior is defined by
  184. :func:`process_exception`. Refer to its documentation for details.
  185. open_timeout: Timeout for opening the connection in seconds.
  186. :obj:`None` disables the timeout.
  187. ping_interval: Interval between keepalive pings in seconds.
  188. :obj:`None` disables keepalive.
  189. ping_timeout: Timeout for keepalive pings in seconds.
  190. :obj:`None` disables timeouts.
  191. close_timeout: Timeout for closing the connection in seconds.
  192. :obj:`None` disables the timeout.
  193. reconnect_delays: Delays in seconds between reconnection attempts.
  194. Default is exponential backoff with 5s jitter, capped at 60s.
  195. max_size: Maximum size of incoming messages in bytes.
  196. :obj:`None` disables the limit. You may pass a ``(max_message_size,
  197. max_fragment_size)`` tuple to set different limits for messages and
  198. fragments when you expect long messages sent in short fragments.
  199. max_queue: High-water mark of the buffer where frames are received.
  200. It defaults to 16 frames. The low-water mark defaults to ``max_queue
  201. // 4``. You may pass a ``(high, low)`` tuple to set the high-water
  202. and low-water marks. If you want to disable flow control entirely,
  203. you may set it to ``None``, although that's a bad idea.
  204. write_limit: High-water mark of write buffer in bytes. It is passed to
  205. :meth:`~asyncio.WriteTransport.set_write_buffer_limits`. It defaults
  206. to 32 KiB. You may pass a ``(high, low)`` tuple to set the
  207. high-water and low-water marks.
  208. logger: Logger for this client.
  209. It defaults to ``logging.getLogger("websockets.client")``.
  210. See the :doc:`logging guide <../../topics/logging>` for details.
  211. create_connection: Factory for the :class:`ClientConnection` managing
  212. the connection. Set it to a wrapper or a subclass to customize
  213. connection handling.
  214. Any other keyword arguments are passed to the event loop's
  215. :meth:`~asyncio.loop.create_connection` method.
  216. For example:
  217. * You can set ``sock`` to provide a preexisting TCP socket. You may call
  218. :func:`socket.create_connection` (not to be confused with the event loop's
  219. :meth:`~asyncio.loop.create_connection` method) to create a suitable
  220. client socket and customize it.
  221. * You can set ``ssl`` to a :class:`~ssl.SSLContext` to enforce TLS settings.
  222. When connecting to a ``wss://`` URI, if ``ssl`` isn't provided, a TLS
  223. context is created with :func:`~ssl.create_default_context`.
  224. * You can set ``server_hostname`` to override the host name from ``uri`` in
  225. the TLS handshake.
  226. * You can configure ``ssl_handshake_timeout`` and ``ssl_shutdown_timeout``.
  227. * You can set ``host`` and ``port`` to connect to a different host and port
  228. from those found in ``uri``. This only changes the destination of the TCP
  229. connection. The host name from ``uri`` is still used in the TLS handshake
  230. for secure connections and in the ``Host`` header.
  231. When using a proxy:
  232. * Prefix keyword arguments with ``proxy_`` for configuring TLS between the
  233. client and an HTTPS proxy: ``proxy_ssl``, ``proxy_server_hostname``,
  234. ``proxy_ssl_handshake_timeout``, and ``proxy_ssl_shutdown_timeout``.
  235. * Use the standard keyword arguments for configuring TLS between the proxy
  236. and the WebSocket server: ``ssl``, ``server_hostname``,
  237. ``ssl_handshake_timeout``, and ``ssl_shutdown_timeout``.
  238. * Other keyword arguments are used only for connecting to the proxy.
  239. Raises:
  240. InvalidURI: If ``uri`` isn't a valid WebSocket URI.
  241. InvalidProxy: If ``proxy`` isn't a valid proxy.
  242. OSError: If the TCP connection fails.
  243. InvalidHandshake: If the opening handshake fails.
  244. TimeoutError: If the opening handshake times out.
  245. """
  246. def __init__(
  247. self,
  248. uri: str,
  249. *,
  250. # WebSocket
  251. origin: Origin | None = None,
  252. extensions: Sequence[ClientExtensionFactory] | None = None,
  253. subprotocols: Sequence[Subprotocol] | None = None,
  254. compression: str | None = "deflate",
  255. # HTTP
  256. additional_headers: HeadersLike | None = None,
  257. user_agent_header: str | None = USER_AGENT,
  258. proxy: str | Literal[True] | None = True,
  259. process_exception: Callable[[Exception], Exception | None] = process_exception,
  260. # Timeouts
  261. open_timeout: float | None = 10,
  262. ping_interval: float | None = 20,
  263. ping_timeout: float | None = 20,
  264. close_timeout: float | None = 10,
  265. reconnect_delays: Callable[[], Generator[float]] = backoff,
  266. # Limits
  267. max_size: int | None | tuple[int | None, int | None] = 2**20,
  268. max_queue: int | None | tuple[int | None, int | None] = 16,
  269. write_limit: int | tuple[int, int | None] = 2**15,
  270. # Logging
  271. logger: LoggerLike | None = None,
  272. # Escape hatches for advanced customization
  273. create_connection: type[ClientConnection] | None = None,
  274. # Other keyword arguments are passed to loop.create_connection
  275. **kwargs: Any,
  276. ) -> None:
  277. self.uri = uri
  278. self.ws_uri = parse_uri(uri)
  279. if not self.ws_uri.secure and kwargs.get("ssl") is not None:
  280. raise ValueError("ssl argument is incompatible with a ws:// URI")
  281. if subprotocols is not None:
  282. validate_subprotocols(subprotocols)
  283. if compression == "deflate":
  284. extensions = enable_client_permessage_deflate(extensions)
  285. elif compression is not None:
  286. raise ValueError(f"unsupported compression: {compression}")
  287. if logger is None:
  288. logger = logging.getLogger("websockets.client")
  289. if create_connection is None:
  290. create_connection = ClientConnection
  291. self.additional_headers = additional_headers
  292. self.user_agent_header = user_agent_header
  293. self.proxy = proxy
  294. self.process_exception = process_exception
  295. self.open_timeout = open_timeout
  296. self.reconnect_delays = reconnect_delays
  297. self.logger = logger
  298. self.create_connection_kwargs = kwargs
  299. def factory(uri: WebSocketURI) -> ClientConnection:
  300. # This is a protocol in the Sans-I/O implementation of websockets.
  301. protocol = ClientProtocol(
  302. uri,
  303. origin=origin,
  304. extensions=extensions,
  305. subprotocols=subprotocols,
  306. max_size=max_size,
  307. logger=logger,
  308. )
  309. # This is a connection in websockets and a protocol in asyncio.
  310. # create_connection defaults to ClientConnection.
  311. connection = create_connection(
  312. protocol,
  313. ping_interval=ping_interval,
  314. ping_timeout=ping_timeout,
  315. close_timeout=close_timeout,
  316. max_queue=max_queue,
  317. write_limit=write_limit,
  318. )
  319. return connection
  320. self.factory = factory
  321. async def open_tcp_connection(self) -> ClientConnection:
  322. """Open a TCP or Unix connection to the server, possibly through a proxy."""
  323. loop = asyncio.get_running_loop()
  324. kwargs = self.create_connection_kwargs.copy()
  325. proxy = self.proxy
  326. if kwargs.get("unix", False):
  327. proxy = None
  328. if kwargs.get("sock") is not None:
  329. proxy = None
  330. if proxy is True:
  331. proxy = get_proxy(self.ws_uri)
  332. def factory() -> ClientConnection:
  333. return self.factory(self.ws_uri)
  334. if self.ws_uri.secure:
  335. kwargs.setdefault("ssl", True)
  336. if kwargs.get("ssl") is None:
  337. raise ValueError("ssl=None is incompatible with a wss:// URI")
  338. kwargs.setdefault("server_hostname", self.ws_uri.host)
  339. if kwargs.pop("unix", False):
  340. _, connection = await loop.create_unix_connection(factory, **kwargs)
  341. return connection
  342. if proxy is not None:
  343. proxy_parsed = parse_proxy(proxy)
  344. if proxy_parsed.scheme[:5] == "socks":
  345. # Connect to the server through the SOCKS proxy.
  346. sock = await connect_socks_proxy(
  347. proxy_parsed,
  348. self.ws_uri,
  349. # python_socks uses local_addr like asyncio.
  350. local_addr=kwargs.pop("local_addr", None),
  351. )
  352. # Initialize WebSocket connection via the proxy.
  353. _, connection = await loop.create_connection(
  354. factory,
  355. sock=sock,
  356. **kwargs,
  357. )
  358. return connection
  359. elif proxy_parsed.scheme[:4] == "http":
  360. # Split keyword arguments between the proxy and the server.
  361. all_kwargs, proxy_kwargs, kwargs = kwargs, {}, {}
  362. for key, value in all_kwargs.items():
  363. if key.startswith("ssl") or key == "server_hostname":
  364. kwargs[key] = value
  365. elif key.startswith("proxy_"):
  366. proxy_kwargs[key[6:]] = value
  367. else:
  368. proxy_kwargs[key] = value
  369. # Validate the proxy_ssl argument.
  370. if proxy_parsed.scheme == "https":
  371. proxy_kwargs.setdefault("ssl", True)
  372. if proxy_kwargs.get("ssl") is None:
  373. raise ValueError(
  374. "proxy_ssl=None is incompatible with an https:// proxy"
  375. )
  376. else:
  377. if proxy_kwargs.get("ssl") is not None:
  378. raise ValueError(
  379. "proxy_ssl argument is incompatible with an http:// proxy"
  380. )
  381. # Connect to the server through the HTTP proxy.
  382. transport = await connect_http_proxy(
  383. proxy_parsed,
  384. self.ws_uri,
  385. user_agent_header=self.user_agent_header,
  386. **proxy_kwargs,
  387. )
  388. # Initialize WebSocket connection via the proxy.
  389. connection = factory()
  390. transport.set_protocol(connection)
  391. ssl = kwargs.pop("ssl", None)
  392. if ssl is True:
  393. ssl = ssl_module.create_default_context()
  394. if ssl is not None:
  395. new_transport = await loop.start_tls(
  396. transport, connection, ssl, **kwargs
  397. )
  398. assert new_transport is not None # help mypy
  399. transport = new_transport
  400. connection.connection_made(transport)
  401. return connection
  402. else:
  403. raise AssertionError("parse_proxy returned unsupported proxy")
  404. else: # proxy is None
  405. # Connect to the server directly.
  406. if kwargs.get("sock") is None:
  407. kwargs.setdefault("host", self.ws_uri.host)
  408. kwargs.setdefault("port", self.ws_uri.port)
  409. # Initialize WebSocket connection.
  410. _, connection = await loop.create_connection(factory, **kwargs)
  411. return connection
  412. def process_redirect(self, exc: Exception) -> Exception | str:
  413. """
  414. Determine whether a connection error is a redirect that can be followed.
  415. Return the new URI if it's a valid redirect. Else, return an exception.
  416. """
  417. if not (
  418. isinstance(exc, InvalidStatus)
  419. and exc.response.status_code
  420. in [
  421. 300, # Multiple Choices
  422. 301, # Moved Permanently
  423. 302, # Found
  424. 303, # See Other
  425. 307, # Temporary Redirect
  426. 308, # Permanent Redirect
  427. ]
  428. and "Location" in exc.response.headers
  429. ):
  430. return exc
  431. old_ws_uri = self.ws_uri
  432. new_uri = urllib.parse.urljoin(self.uri, exc.response.headers["Location"])
  433. new_ws_uri = parse_uri(new_uri)
  434. # If connect() received a socket, it is closed and cannot be reused.
  435. if self.create_connection_kwargs.get("sock") is not None:
  436. return ValueError(
  437. f"cannot follow redirect to {new_uri} with a preexisting socket"
  438. )
  439. # TLS downgrade is forbidden.
  440. if old_ws_uri.secure and not new_ws_uri.secure:
  441. return SecurityError(f"cannot follow redirect to non-secure URI {new_uri}")
  442. # Apply restrictions to cross-origin redirects.
  443. if (
  444. old_ws_uri.secure != new_ws_uri.secure
  445. or old_ws_uri.host != new_ws_uri.host
  446. or old_ws_uri.port != new_ws_uri.port
  447. ):
  448. # Cross-origin redirects on Unix sockets don't quite make sense.
  449. if self.create_connection_kwargs.get("unix", False):
  450. return ValueError(
  451. f"cannot follow cross-origin redirect to {new_uri} "
  452. f"with a Unix socket"
  453. )
  454. # Cross-origin redirects when host and port are overridden are ill-defined.
  455. if (
  456. self.create_connection_kwargs.get("host") is not None
  457. or self.create_connection_kwargs.get("port") is not None
  458. ):
  459. return ValueError(
  460. f"cannot follow cross-origin redirect to {new_uri} "
  461. f"with an explicit host or port"
  462. )
  463. # Strip credentials to avoid leaking them to a different origin.
  464. if self.additional_headers is not None:
  465. self.additional_headers = Headers(
  466. (
  467. (key, value)
  468. for key, value in Headers(self.additional_headers).raw_items()
  469. if key.lower()
  470. not in ["authorization", "cookie", "proxy-authorization"]
  471. )
  472. )
  473. return new_uri
  474. # ... = await connect(...)
  475. def __await__(self) -> Generator[Any, None, ClientConnection]:
  476. # Create a suitable iterator by calling __await__ on a coroutine.
  477. return self.__await_impl__().__await__()
  478. async def __await_impl__(self) -> ClientConnection:
  479. try:
  480. async with asyncio.timeout(self.open_timeout):
  481. for _ in range(MAX_REDIRECTS):
  482. connection = await self.open_tcp_connection()
  483. try:
  484. await connection.handshake(
  485. self.additional_headers,
  486. self.user_agent_header,
  487. )
  488. except asyncio.CancelledError:
  489. connection.transport.abort()
  490. raise
  491. except Exception as exc:
  492. # Always close the connection even though keep-alive is
  493. # the default in HTTP/1.1 because create_connection ties
  494. # opening the network connection with initializing the
  495. # protocol. In the current design of connect(), there is
  496. # no easy way to reuse the network connection that works
  497. # in every case nor to reinitialize the protocol.
  498. connection.transport.abort()
  499. exc_or_uri = self.process_redirect(exc)
  500. if isinstance(exc_or_uri, Exception):
  501. # Response isn't a valid redirect; raise the exception.
  502. if exc_or_uri is exc:
  503. raise
  504. else:
  505. raise exc_or_uri from exc
  506. else:
  507. # Response is a valid redirect; follow it.
  508. self.uri = exc_or_uri
  509. self.ws_uri = parse_uri(exc_or_uri)
  510. continue
  511. else:
  512. connection.start_keepalive()
  513. return connection
  514. else:
  515. raise SecurityError(f"more than {MAX_REDIRECTS} redirects")
  516. except TimeoutError as exc:
  517. # Re-raise exception with an informative error message.
  518. raise TimeoutError("timed out during opening handshake") from exc
  519. # async with connect(...) as ...: ...
  520. async def __aenter__(self) -> ClientConnection:
  521. if hasattr(self, "connection"):
  522. raise RuntimeError("connect() isn't reentrant")
  523. self.connection = await self
  524. return self.connection
  525. async def __aexit__(
  526. self,
  527. exc_type: type[BaseException] | None,
  528. exc_value: BaseException | None,
  529. traceback: TracebackType | None,
  530. ) -> None:
  531. try:
  532. await self.connection.close()
  533. finally:
  534. del self.connection
  535. # async for ... in connect(...):
  536. async def __aiter__(self) -> AsyncIterator[ClientConnection]:
  537. delays: Generator[float] | None = None
  538. while True:
  539. try:
  540. async with self as connection:
  541. yield connection
  542. except Exception as exc:
  543. # Determine whether the exception is retryable or fatal.
  544. # The API of process_exception is "return an exception or None";
  545. # "raise an exception" is also supported because it's a frequent
  546. # mistake. It isn't documented in order to keep the API simple.
  547. try:
  548. new_exc = self.process_exception(exc)
  549. except Exception as raised_exc:
  550. new_exc = raised_exc
  551. # The connection failed with a fatal error.
  552. # Raise the exception and exit the loop.
  553. if new_exc is exc:
  554. raise
  555. if new_exc is not None:
  556. raise new_exc from exc
  557. # The connection failed with a retryable error.
  558. # Start or continue backoff and reconnect.
  559. if delays is None:
  560. delays = self.reconnect_delays()
  561. delay = next(delays)
  562. self.logger.info(
  563. "connect failed; reconnecting in %.1f seconds: %s",
  564. delay,
  565. traceback.format_exception_only(exc)[0].strip(),
  566. )
  567. await asyncio.sleep(delay)
  568. else:
  569. # The connection succeeded. Reset backoff.
  570. delays = None
  571. def unix_connect(
  572. path: str | None = None,
  573. uri: str | None = None,
  574. **kwargs: Any,
  575. ) -> connect:
  576. """
  577. Connect to a WebSocket server listening on a Unix socket.
  578. This function accepts the same keyword arguments as :func:`connect`.
  579. It's only available on Unix.
  580. It's mainly useful for debugging servers listening on Unix sockets.
  581. Args:
  582. path: File system path to the Unix socket.
  583. uri: URI of the WebSocket server. ``uri`` defaults to
  584. ``ws://localhost/`` or, when a ``ssl`` argument is provided, to
  585. ``wss://localhost/``.
  586. """
  587. if uri is None:
  588. if kwargs.get("ssl") is None:
  589. uri = "ws://localhost/"
  590. else:
  591. uri = "wss://localhost/"
  592. return connect(uri=uri, unix=True, path=path, **kwargs)
  593. try:
  594. from python_socks import ProxyType
  595. from python_socks.async_.asyncio import Proxy as SocksProxy
  596. except ImportError:
  597. async def connect_socks_proxy(
  598. proxy: Proxy,
  599. ws_uri: WebSocketURI,
  600. **kwargs: Any,
  601. ) -> socket.socket:
  602. raise ImportError("connecting through a SOCKS proxy requires python-socks")
  603. else:
  604. SOCKS_PROXY_TYPES = {
  605. "socks5h": ProxyType.SOCKS5,
  606. "socks5": ProxyType.SOCKS5,
  607. "socks4a": ProxyType.SOCKS4,
  608. "socks4": ProxyType.SOCKS4,
  609. }
  610. SOCKS_PROXY_RDNS = {
  611. "socks5h": True,
  612. "socks5": False,
  613. "socks4a": True,
  614. "socks4": False,
  615. }
  616. async def connect_socks_proxy(
  617. proxy: Proxy,
  618. ws_uri: WebSocketURI,
  619. **kwargs: Any,
  620. ) -> socket.socket:
  621. """Connect via a SOCKS proxy and return the socket."""
  622. socks_proxy = SocksProxy(
  623. SOCKS_PROXY_TYPES[proxy.scheme],
  624. proxy.host,
  625. proxy.port,
  626. proxy.username,
  627. proxy.password,
  628. SOCKS_PROXY_RDNS[proxy.scheme],
  629. )
  630. # connect() is documented to raise OSError.
  631. # socks_proxy.connect() doesn't raise TimeoutError; it gets canceled.
  632. # Wrap other exceptions in ProxyError, a subclass of InvalidHandshake.
  633. try:
  634. return await socks_proxy.connect(ws_uri.host, ws_uri.port, **kwargs)
  635. except OSError:
  636. raise
  637. except Exception as exc:
  638. raise ProxyError("failed to connect to SOCKS proxy") from exc
  639. class HTTPProxyConnection(asyncio.Protocol):
  640. def __init__(
  641. self,
  642. ws_uri: WebSocketURI,
  643. proxy: Proxy,
  644. user_agent_header: str | None = None,
  645. ):
  646. self.ws_uri = ws_uri
  647. self.proxy = proxy
  648. self.user_agent_header = user_agent_header
  649. self.reader = StreamReader()
  650. self.parser = Response.parse(
  651. self.reader.read_line,
  652. self.reader.read_exact,
  653. self.reader.read_to_eof,
  654. proxy=True,
  655. )
  656. loop = asyncio.get_running_loop()
  657. self.response: asyncio.Future[Response] = loop.create_future()
  658. def run_parser(self) -> None:
  659. try:
  660. next(self.parser)
  661. except StopIteration as exc:
  662. response = exc.value
  663. if 200 <= response.status_code < 300:
  664. self.response.set_result(response)
  665. else:
  666. self.response.set_exception(InvalidProxyStatus(response))
  667. except Exception as exc:
  668. proxy_exc = InvalidProxyMessage(
  669. "did not receive a valid HTTP response from proxy"
  670. )
  671. proxy_exc.__cause__ = exc
  672. self.response.set_exception(proxy_exc)
  673. def connection_made(self, transport: asyncio.BaseTransport) -> None:
  674. transport = cast(asyncio.Transport, transport)
  675. self.transport = transport
  676. self.transport.write(
  677. prepare_connect_request(self.proxy, self.ws_uri, self.user_agent_header)
  678. )
  679. def data_received(self, data: bytes) -> None:
  680. self.reader.feed_data(data)
  681. self.run_parser()
  682. def eof_received(self) -> None:
  683. self.reader.feed_eof()
  684. self.run_parser()
  685. def connection_lost(self, exc: Exception | None) -> None:
  686. self.reader.feed_eof()
  687. self.run_parser()
  688. async def connect_http_proxy(
  689. proxy: Proxy,
  690. ws_uri: WebSocketURI,
  691. *,
  692. user_agent_header: str | None = None,
  693. **kwargs: Any,
  694. ) -> asyncio.Transport:
  695. transport, protocol = await asyncio.get_running_loop().create_connection(
  696. lambda: HTTPProxyConnection(ws_uri, proxy, user_agent_header),
  697. proxy.host,
  698. proxy.port,
  699. **kwargs,
  700. )
  701. try:
  702. # This raises exceptions if the connection to the proxy fails.
  703. await protocol.response
  704. except (asyncio.CancelledError, Exception):
  705. transport.abort()
  706. raise
  707. return transport