client.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import socket
  5. import ssl as ssl_module
  6. import threading
  7. import time
  8. import traceback
  9. import urllib.parse
  10. import warnings
  11. from collections.abc import Generator, Iterator, Sequence
  12. from types import TracebackType
  13. from typing import Any, Callable, Literal, TypeVar, cast, overload
  14. from ..client import ClientProtocol, backoff, process_exception
  15. from ..datastructures import Headers, HeadersLike
  16. from ..exceptions import (
  17. InvalidProxyMessage,
  18. InvalidProxyStatus,
  19. InvalidStatus,
  20. ProxyError,
  21. SecurityError,
  22. )
  23. from ..extensions.base import ClientExtensionFactory
  24. from ..extensions.permessage_deflate import enable_client_permessage_deflate
  25. from ..headers import validate_subprotocols
  26. from ..http11 import USER_AGENT, Response
  27. from ..protocol import CONNECTING, Event
  28. from ..proxy import Proxy, get_proxy, parse_proxy, prepare_connect_request
  29. from ..streams import StreamReader
  30. from ..typing import BytesLike, LoggerLike, Origin, PathLike, Subprotocol
  31. from ..uri import WebSocketURI, parse_uri
  32. from .connection import Connection
  33. from .utils import Deadline
  34. __all__ = ["connect", "unix_connect", "reconnect", "unix_reconnect", "ClientConnection"]
  35. MAX_REDIRECTS = int(os.environ.get("WEBSOCKETS_MAX_REDIRECTS", "10"))
  36. class ClientConnection(Connection):
  37. """
  38. :mod:`threading` implementation of a WebSocket client connection.
  39. :class:`ClientConnection` provides :meth:`recv` and :meth:`send` methods for
  40. receiving and sending messages.
  41. It supports iteration to receive messages::
  42. for message in websocket:
  43. process(message)
  44. The iterator exits normally when the connection is closed with code
  45. 1000 (OK) or 1001 (going away) or without a close code. It raises a
  46. :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
  47. closed with any other code.
  48. The ``ping_interval``, ``ping_timeout``, ``close_timeout``, and
  49. ``max_queue`` arguments have the same meaning as in :func:`connect`.
  50. Args:
  51. socket: Socket connected to a WebSocket server.
  52. protocol: Sans-I/O connection.
  53. """
  54. def __init__(
  55. self,
  56. sock: socket.socket,
  57. protocol: ClientProtocol,
  58. *,
  59. ping_interval: float | None = 20,
  60. ping_timeout: float | None = 20,
  61. close_timeout: float | None = 10,
  62. max_queue: int | None | tuple[int | None, int | None] = 16,
  63. ) -> None:
  64. self.protocol: ClientProtocol
  65. self.response_rcvd = threading.Event()
  66. self.pending_legacy_warning = True
  67. super().__init__(
  68. sock,
  69. protocol,
  70. ping_interval=ping_interval,
  71. ping_timeout=ping_timeout,
  72. close_timeout=close_timeout,
  73. max_queue=max_queue,
  74. )
  75. def __enter__(self) -> ClientConnection:
  76. self.pending_legacy_warning = False
  77. return super().__enter__()
  78. def maybe_raise_legacy_warning(self) -> None:
  79. if self.pending_legacy_warning:
  80. self.pending_legacy_warning = False
  81. warnings.warn( # deprecated in 17.1
  82. "connect() must be used as a context manager: "
  83. "with connect(...) as websocket: ...; alternatively, use "
  84. "websocket = connect(..., legacy=True) to connect directly",
  85. DeprecationWarning,
  86. stacklevel=3,
  87. )
  88. def handshake(
  89. self,
  90. additional_headers: HeadersLike | None = None,
  91. user_agent_header: str | None = USER_AGENT,
  92. timeout: float | None = None,
  93. ) -> None:
  94. """
  95. Perform the opening handshake.
  96. """
  97. self.request = self.protocol.connect()
  98. if additional_headers is not None:
  99. self.request.headers.update(additional_headers)
  100. if user_agent_header is not None:
  101. self.request.headers.setdefault("User-Agent", user_agent_header)
  102. with self.send_context(expected_state=CONNECTING):
  103. self.protocol.send_request(self.request)
  104. if not self.response_rcvd.wait(timeout):
  105. raise TimeoutError("timed out while waiting for handshake response")
  106. # self.protocol.handshake_exc is set when the connection is lost before
  107. # receiving a response, when the response cannot be parsed, or when the
  108. # response fails the handshake.
  109. if self.protocol.handshake_exc is not None:
  110. raise self.protocol.handshake_exc
  111. def process_event(self, event: Event) -> None:
  112. """
  113. Process one incoming event.
  114. """
  115. # First event - handshake response.
  116. if self.response is None:
  117. assert isinstance(event, Response)
  118. self.response = event
  119. self.response_rcvd.set()
  120. # Later events - frames.
  121. else:
  122. super().process_event(event)
  123. def recv_events(self) -> None:
  124. """
  125. Read incoming data from the socket and process events.
  126. """
  127. try:
  128. super().recv_events()
  129. finally:
  130. # If the connection is closed during the handshake, unblock it.
  131. self.response_rcvd.set()
  132. class reconnect:
  133. """
  134. Similar to :func:`connect`, with support for automatic reconnection.
  135. :func:`reconnect` can also be treated as an infinite iterator to reconnect
  136. automatically on errors::
  137. for websocket in reconnect(...):
  138. try:
  139. ...
  140. except websockets.exceptions.ConnectionClosed:
  141. continue
  142. If the connection fails with a transient error, it is retried with
  143. exponential backoff. If it fails with a fatal error, the exception is
  144. raised, breaking out of the loop.
  145. The connection is closed automatically after each iteration of the loop.
  146. :func:`reconnect` accepts the same arguments as :func:`connect`, minus the
  147. ``legacy`` flag, plus those listed below. It raises the same exceptions.
  148. Args:
  149. process_exception: When reconnecting automatically, tell whether an
  150. error is transient or fatal. The default behavior is defined by
  151. :func:`~websockets.client.process_exception`. Refer to its
  152. documentation for details.
  153. reconnect_delays: Delays in seconds between reconnection attempts.
  154. Default is exponential backoff with 5s jitter, capped at 60s.
  155. .. admonition:: Why is :func:`reconnect` a separate API from :func:`connect`?
  156. :class: tip
  157. A new API was necessary to maintain backwards compatibility with this
  158. historical behavior of :func:`connect`::
  159. websocket = connect(...)
  160. for message in websocket:
  161. ...
  162. Once the deprecation period elapses, :func:`connect` will be changed to
  163. behave like :func:`reconnect` by default.
  164. """
  165. def __init__(
  166. self,
  167. uri: str,
  168. *,
  169. # TCP/TLS
  170. sock: socket.socket | None = None,
  171. ssl: ssl_module.SSLContext | None = None,
  172. server_hostname: str | None = None,
  173. # WebSocket
  174. origin: Origin | None = None,
  175. extensions: Sequence[ClientExtensionFactory] | None = None,
  176. subprotocols: Sequence[Subprotocol] | None = None,
  177. compression: str | None = "deflate",
  178. # HTTP
  179. additional_headers: HeadersLike | None = None,
  180. user_agent_header: str | None = USER_AGENT,
  181. proxy: str | Literal[True] | None = True,
  182. proxy_ssl: ssl_module.SSLContext | None = None,
  183. proxy_server_hostname: str | None = None,
  184. process_exception: Callable[[Exception], Exception | None] = process_exception,
  185. # Timeouts
  186. open_timeout: float | None = 10,
  187. ping_interval: float | None = 20,
  188. ping_timeout: float | None = 20,
  189. close_timeout: float | None = 10,
  190. reconnect_delays: Callable[[], Generator[float]] = backoff,
  191. # Limits
  192. max_size: int | None | tuple[int | None, int | None] = 2**20,
  193. max_queue: int | None | tuple[int | None, int | None] = 16,
  194. # Logging
  195. logger: LoggerLike | None = None,
  196. # Escape hatch for advanced customization
  197. create_connection: type[ClientConnection] | None = None,
  198. # Other keyword arguments are passed to socket.create_connection
  199. **kwargs: Any,
  200. ) -> None:
  201. # Backwards compatibility: ssl used to be called ssl_context.
  202. if ssl is None and "ssl_context" in kwargs:
  203. ssl = kwargs.pop("ssl_context")
  204. warnings.warn( # deprecated in 13.0 - 2024-08-20
  205. "ssl_context was renamed to ssl",
  206. DeprecationWarning,
  207. )
  208. self.uri = uri
  209. self.ws_uri = parse_uri(uri)
  210. if not self.ws_uri.secure and ssl is not None:
  211. raise ValueError("ssl argument is incompatible with a ws:// URI")
  212. if subprotocols is not None:
  213. validate_subprotocols(subprotocols)
  214. if compression == "deflate":
  215. extensions = enable_client_permessage_deflate(extensions)
  216. elif compression is not None:
  217. raise ValueError(f"unsupported compression: {compression}")
  218. if logger is None:
  219. logger = logging.getLogger("websockets.client")
  220. if create_connection is None:
  221. create_connection = ClientConnection
  222. self.sock = sock
  223. self.ssl = ssl
  224. self.server_hostname = server_hostname
  225. self.additional_headers = additional_headers
  226. self.user_agent_header = user_agent_header
  227. self.proxy = proxy
  228. self.proxy_ssl = proxy_ssl
  229. self.proxy_server_hostname = proxy_server_hostname
  230. self.process_exception = process_exception
  231. self.open_timeout = open_timeout
  232. self.reconnect_delays = reconnect_delays
  233. self.logger = logger
  234. self.create_connection = create_connection
  235. self.open_socket_kwargs = kwargs
  236. self.protocol_kwargs = dict(
  237. origin=origin,
  238. extensions=extensions,
  239. subprotocols=subprotocols,
  240. max_size=max_size,
  241. logger=logger,
  242. )
  243. self.connection_kwargs = dict(
  244. ping_interval=ping_interval,
  245. ping_timeout=ping_timeout,
  246. close_timeout=close_timeout,
  247. max_queue=max_queue,
  248. )
  249. def open_socket(self, deadline: Deadline) -> socket.socket:
  250. """Open a TCP or Unix connection to the server, possibly through a proxy."""
  251. kwargs = self.open_socket_kwargs.copy()
  252. unix = kwargs.pop("unix", False)
  253. proxy = self.proxy
  254. if unix:
  255. proxy = None
  256. if proxy is True:
  257. proxy = get_proxy(self.ws_uri)
  258. if unix:
  259. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  260. try:
  261. sock.settimeout(deadline.timeout())
  262. sock.connect(os.fspath(kwargs.pop("path")))
  263. except Exception:
  264. sock.close()
  265. raise
  266. elif proxy is not None:
  267. proxy_parsed = parse_proxy(proxy)
  268. if proxy_parsed.scheme[:5] == "socks":
  269. sock = connect_socks_proxy(
  270. proxy_parsed,
  271. self.ws_uri,
  272. deadline,
  273. # websockets is consistent with the socket module while
  274. # python_socks is consistent across implementations.
  275. local_addr=kwargs.pop("source_address", None),
  276. )
  277. elif proxy_parsed.scheme[:4] == "http":
  278. if proxy_parsed.scheme != "https" and self.proxy_ssl is not None:
  279. raise ValueError(
  280. "proxy_ssl argument is incompatible with an http:// proxy"
  281. )
  282. sock = connect_http_proxy(
  283. proxy_parsed,
  284. self.ws_uri,
  285. deadline,
  286. user_agent_header=self.user_agent_header,
  287. ssl=self.proxy_ssl,
  288. server_hostname=self.proxy_server_hostname,
  289. **kwargs,
  290. )
  291. else:
  292. raise AssertionError("parse_proxy returned unsupported proxy")
  293. else: # proxy is None
  294. kwargs.setdefault("address", (self.ws_uri.host, self.ws_uri.port))
  295. kwargs.setdefault("timeout", deadline.timeout())
  296. sock = socket.create_connection(**kwargs)
  297. sock.settimeout(None)
  298. return sock
  299. def enable_tls(self, sock: socket.socket, deadline: Deadline) -> socket.socket:
  300. """Enable TLS on the connection."""
  301. if self.ssl is None:
  302. ssl = ssl_module.create_default_context()
  303. else:
  304. ssl = self.ssl
  305. if self.server_hostname is None:
  306. server_hostname = self.ws_uri.host
  307. else:
  308. server_hostname = self.server_hostname
  309. sock.settimeout(deadline.timeout())
  310. if self.proxy_ssl is None:
  311. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  312. else:
  313. sock_2 = SSLSSLSocket(sock, ssl, server_hostname=server_hostname)
  314. # Let's pretend that sock is a socket, even though it isn't.
  315. sock = cast(socket.socket, sock_2)
  316. sock.settimeout(None)
  317. return sock
  318. def open_connection(self, deadline: Deadline) -> ClientConnection:
  319. """Create a WebSocket connection."""
  320. if self.sock is None:
  321. sock = self.open_socket(deadline)
  322. else:
  323. sock = self.sock
  324. try:
  325. if sock.family in {socket.AF_INET, socket.AF_INET6}:
  326. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
  327. if self.ws_uri.secure:
  328. sock = self.enable_tls(sock, deadline)
  329. protocol = ClientProtocol(
  330. self.ws_uri,
  331. **self.protocol_kwargs, # type: ignore
  332. )
  333. # self.create_connection defaults to ClientConnection.
  334. connection = self.create_connection(
  335. sock,
  336. protocol,
  337. **self.connection_kwargs, # type: ignore
  338. )
  339. except Exception:
  340. sock.close()
  341. raise
  342. try:
  343. connection.handshake(
  344. self.additional_headers,
  345. self.user_agent_header,
  346. deadline.timeout(),
  347. )
  348. except Exception:
  349. connection.close_socket()
  350. connection.recv_events_thread.join()
  351. raise
  352. return connection
  353. def process_redirect(self, exc: Exception) -> Exception | str:
  354. """
  355. Determine whether a connection error is a redirect that can be followed.
  356. Return the new URI if it's a valid redirect. Else, return an exception.
  357. """
  358. if not (
  359. isinstance(exc, InvalidStatus)
  360. and exc.response.status_code
  361. in [
  362. 300, # Multiple Choices
  363. 301, # Moved Permanently
  364. 302, # Found
  365. 303, # See Other
  366. 307, # Temporary Redirect
  367. 308, # Permanent Redirect
  368. ]
  369. and "Location" in exc.response.headers
  370. ):
  371. return exc
  372. old_ws_uri = self.ws_uri
  373. new_uri = urllib.parse.urljoin(self.uri, exc.response.headers["Location"])
  374. new_ws_uri = parse_uri(new_uri)
  375. # If connect() received a socket, it is closed and cannot be reused.
  376. if self.sock is not None:
  377. return ValueError(
  378. f"cannot follow redirect to {new_uri} with a preexisting socket"
  379. )
  380. # TLS downgrade is forbidden.
  381. if old_ws_uri.secure and not new_ws_uri.secure:
  382. return SecurityError(f"cannot follow redirect to non-secure URI {new_uri}")
  383. # Apply restrictions to cross-origin redirects.
  384. if (
  385. old_ws_uri.secure != new_ws_uri.secure
  386. or old_ws_uri.host != new_ws_uri.host
  387. or old_ws_uri.port != new_ws_uri.port
  388. ):
  389. # Cross-origin redirects on Unix sockets don't quite make sense.
  390. if self.open_socket_kwargs.get("unix", False):
  391. return ValueError(
  392. f"cannot follow cross-origin redirect to {new_uri} "
  393. f"with a Unix socket"
  394. )
  395. # Cross-origin redirects when host and port are overridden are ill-defined.
  396. if self.open_socket_kwargs.get("address") is not None:
  397. return ValueError(
  398. f"cannot follow cross-origin redirect to {new_uri} "
  399. f"with an explicit host and port"
  400. )
  401. # Strip credentials to avoid leaking them to a different origin.
  402. if self.additional_headers is not None:
  403. self.additional_headers = Headers(
  404. (
  405. (key, value)
  406. for key, value in Headers(self.additional_headers).raw_items()
  407. if key.lower()
  408. not in ["authorization", "cookie", "proxy-authorization"]
  409. )
  410. )
  411. return new_uri
  412. def connect(self) -> ClientConnection:
  413. """Connect to a WebSocket server, following redirects."""
  414. # Calculate timeouts on the TCP, TLS, and WebSocket handshakes.
  415. # The TCP and TLS timeouts must be set on the socket, then removed
  416. # to avoid conflicting with the WebSocket timeout in handshake().
  417. deadline = Deadline(self.open_timeout)
  418. for _ in range(MAX_REDIRECTS):
  419. try:
  420. connection = self.open_connection(deadline)
  421. except Exception as exc:
  422. exc_or_uri = self.process_redirect(exc)
  423. if isinstance(exc_or_uri, Exception):
  424. # Response isn't a valid redirect; raise the exception.
  425. if exc_or_uri is exc:
  426. raise
  427. else:
  428. raise exc_or_uri from exc
  429. else:
  430. # Response is a valid redirect; follow it.
  431. self.uri = exc_or_uri
  432. self.ws_uri = parse_uri(exc_or_uri)
  433. continue
  434. else:
  435. connection.start_keepalive()
  436. return connection
  437. else:
  438. raise SecurityError(f"more than {MAX_REDIRECTS} redirects")
  439. # with connect(...) as ...: ...
  440. def __enter__(self) -> ClientConnection:
  441. if hasattr(self, "connection"):
  442. raise RuntimeError("connect() isn't reentrant")
  443. self.connection = self.connect()
  444. self.connection.pending_legacy_warning = False
  445. return self.connection
  446. def __exit__(
  447. self,
  448. exc_type: type[BaseException] | None,
  449. exc_value: BaseException | None,
  450. exc_traceback: TracebackType | None,
  451. ) -> None:
  452. try:
  453. self.connection.close()
  454. finally:
  455. del self.connection
  456. # for ... in reconnect(...): ...
  457. def __iter__(self) -> Iterator[ClientConnection]:
  458. delays: Generator[float] | None = None
  459. while True:
  460. try:
  461. with self as connection:
  462. yield connection
  463. except Exception as exc:
  464. # Determine whether the exception is retryable or fatal.
  465. # The API of process_exception is "return an exception or None";
  466. # "raise an exception" is also supported because it's a frequent
  467. # mistake. It isn't documented in order to keep the API simple.
  468. try:
  469. new_exc = self.process_exception(exc)
  470. except Exception as raised_exc:
  471. new_exc = raised_exc
  472. # The connection failed with a fatal error.
  473. # Raise the exception and exit the loop.
  474. if new_exc is exc:
  475. raise
  476. if new_exc is not None:
  477. raise new_exc from exc
  478. # The connection failed with a retryable error.
  479. # Start or continue backoff and reconnect.
  480. if delays is None:
  481. delays = self.reconnect_delays()
  482. delay = next(delays)
  483. self.logger.info(
  484. "connect failed; reconnecting in %.1f seconds: %s",
  485. delay,
  486. traceback.format_exception_only(exc)[0].strip(),
  487. )
  488. time.sleep(delay)
  489. else:
  490. # The connection succeeded. Reset backoff.
  491. delays = None
  492. @overload
  493. def connect(
  494. uri: str,
  495. *,
  496. # TCP/TLS
  497. sock: socket.socket | None = ...,
  498. ssl: ssl_module.SSLContext | None = ...,
  499. server_hostname: str | None = ...,
  500. # WebSocket
  501. origin: Origin | None = ...,
  502. extensions: Sequence[ClientExtensionFactory] | None = ...,
  503. subprotocols: Sequence[Subprotocol] | None = ...,
  504. compression: str | None = ...,
  505. # HTTP
  506. additional_headers: HeadersLike | None = ...,
  507. user_agent_header: str | None = ...,
  508. proxy: str | Literal[True] | None = ...,
  509. proxy_ssl: ssl_module.SSLContext | None = ...,
  510. proxy_server_hostname: str | None = ...,
  511. # Timeouts
  512. open_timeout: float | None = ...,
  513. ping_interval: float | None = ...,
  514. ping_timeout: float | None = ...,
  515. close_timeout: float | None = ...,
  516. # Limits
  517. max_size: int | None | tuple[int | None, int | None] = ...,
  518. max_queue: int | None | tuple[int | None, int | None] = ...,
  519. # Logging
  520. logger: LoggerLike | None = ...,
  521. # Escape hatch for advanced customization
  522. create_connection: type[ClientConnection] | None = ...,
  523. # Backwards and forwards compatibility
  524. legacy: Literal[True] | None = ...,
  525. # Other keyword arguments are passed to socket.create_connection
  526. **kwargs: Any,
  527. ) -> ClientConnection: ...
  528. @overload
  529. def connect(
  530. uri: str,
  531. *,
  532. # TCP/TLS
  533. sock: socket.socket | None = ...,
  534. ssl: ssl_module.SSLContext | None = ...,
  535. server_hostname: str | None = ...,
  536. # WebSocket
  537. origin: Origin | None = ...,
  538. extensions: Sequence[ClientExtensionFactory] | None = ...,
  539. subprotocols: Sequence[Subprotocol] | None = ...,
  540. compression: str | None = ...,
  541. # HTTP
  542. additional_headers: HeadersLike | None = ...,
  543. user_agent_header: str | None = ...,
  544. proxy: str | Literal[True] | None = ...,
  545. proxy_ssl: ssl_module.SSLContext | None = ...,
  546. proxy_server_hostname: str | None = ...,
  547. # Timeouts
  548. open_timeout: float | None = ...,
  549. ping_interval: float | None = ...,
  550. ping_timeout: float | None = ...,
  551. close_timeout: float | None = ...,
  552. # Limits
  553. max_size: int | None | tuple[int | None, int | None] = ...,
  554. max_queue: int | None | tuple[int | None, int | None] = ...,
  555. # Logging
  556. logger: LoggerLike | None = ...,
  557. # Escape hatch for advanced customization
  558. create_connection: type[ClientConnection] | None = ...,
  559. # Backwards and forwards compatibility
  560. legacy: Literal[False],
  561. # Other keyword arguments are passed to socket.create_connection
  562. **kwargs: Any,
  563. ) -> reconnect: ...
  564. def connect(
  565. uri: str,
  566. *,
  567. # TCP/TLS
  568. sock: socket.socket | None = None,
  569. ssl: ssl_module.SSLContext | None = None,
  570. server_hostname: str | None = None,
  571. # WebSocket
  572. origin: Origin | None = None,
  573. extensions: Sequence[ClientExtensionFactory] | None = None,
  574. subprotocols: Sequence[Subprotocol] | None = None,
  575. compression: str | None = "deflate",
  576. # HTTP
  577. additional_headers: HeadersLike | None = None,
  578. user_agent_header: str | None = USER_AGENT,
  579. proxy: str | Literal[True] | None = True,
  580. proxy_ssl: ssl_module.SSLContext | None = None,
  581. proxy_server_hostname: str | None = None,
  582. # Timeouts
  583. open_timeout: float | None = 10,
  584. ping_interval: float | None = 20,
  585. ping_timeout: float | None = 20,
  586. close_timeout: float | None = 10,
  587. # Limits
  588. max_size: int | None | tuple[int | None, int | None] = 2**20,
  589. max_queue: int | None | tuple[int | None, int | None] = 16,
  590. # Logging
  591. logger: LoggerLike | None = None,
  592. # Escape hatch for advanced customization
  593. create_connection: type[ClientConnection] | None = None,
  594. # Backwards and forwards compatibility
  595. legacy: bool | None = None,
  596. # Other keyword arguments are passed to socket.create_connection
  597. **kwargs: Any,
  598. ) -> ClientConnection | reconnect:
  599. """
  600. Connect to the WebSocket server at ``uri``.
  601. :func:`connect` should be treated as a context manager yielding a
  602. :class:`ClientConnection`, which can then receive and send messages::
  603. from websockets.sync.client import connect
  604. with connect(...) as websocket:
  605. ...
  606. The connection is closed automatically when exiting the context.
  607. Use :func:`reconnect` to reconnect automatically on errors.
  608. For backwards compatibility, :func:`connect` can be called directly::
  609. websocket = connect(..., legacy=True)
  610. In that case, you're responsible for closing the connection with
  611. :meth:`ClientConnection.close` when no longer needed.
  612. When the ``legacy`` flag is enabled, :func:`connect` returns directly a
  613. :class:`ClientConnection` and iterating that connection yields messages.
  614. Currently, this is the default behavior when ``legacy`` isn't specified.
  615. When the ``legacy`` flag is explicitly disabled, :func:`connect` behaves
  616. like :func:`reconnect`: using it as an iterator returns a new connection
  617. at each iteration, making it easy to reconnect automatically on errors.
  618. Args:
  619. uri: URI of the WebSocket server.
  620. sock: Preexisting TCP socket. ``sock`` overrides the host and port
  621. from ``uri``. You may call :func:`socket.create_connection` to
  622. create a suitable TCP socket.
  623. ssl: Configuration for enabling TLS on the connection.
  624. server_hostname: Host name for the TLS handshake. ``server_hostname``
  625. overrides the host name from ``uri``.
  626. origin: Value of the ``Origin`` header, for servers that require it.
  627. extensions: List of supported extensions, in order in which they
  628. should be negotiated and run.
  629. subprotocols: List of supported subprotocols, in order of decreasing
  630. preference.
  631. compression: The "permessage-deflate" extension is enabled by default.
  632. Set ``compression`` to :obj:`None` to disable it. See the
  633. :doc:`compression guide <../../topics/compression>` for details.
  634. additional_headers: Arbitrary HTTP headers to add to the handshake
  635. request.
  636. user_agent_header: Value of the ``User-Agent`` request header.
  637. It defaults to ``"Python/x.y.z websockets/X.Y"``.
  638. Setting it to :obj:`None` removes the header.
  639. proxy: If a proxy is configured, it is used by default. Set ``proxy``
  640. to :obj:`None` to disable the proxy or to the address of a proxy
  641. to override the system configuration. See the :doc:`proxy docs
  642. <../../topics/proxies>` for details.
  643. proxy_ssl: Configuration for enabling TLS on the proxy connection.
  644. proxy_server_hostname: Host name for the TLS handshake with the proxy.
  645. ``proxy_server_hostname`` overrides the host name from ``proxy``.
  646. open_timeout: Timeout for opening the connection in seconds.
  647. :obj:`None` disables the timeout.
  648. ping_interval: Interval between keepalive pings in seconds.
  649. :obj:`None` disables keepalive.
  650. ping_timeout: Timeout for keepalive pings in seconds.
  651. :obj:`None` disables timeouts.
  652. close_timeout: Timeout for closing the connection in seconds.
  653. :obj:`None` disables the timeout.
  654. max_size: Maximum size of incoming messages in bytes.
  655. :obj:`None` disables the limit. You may pass a ``(max_message_size,
  656. max_fragment_size)`` tuple to set different limits for messages and
  657. fragments when you expect long messages sent in short fragments.
  658. max_queue: High-water mark of the buffer where frames are received.
  659. It defaults to 16 frames. The low-water mark defaults to ``max_queue
  660. // 4``. You may pass a ``(high, low)`` tuple to set the high-water
  661. and low-water marks. If you want to disable flow control entirely,
  662. you may set it to ``None``, although that's a bad idea.
  663. logger: Logger for this client.
  664. It defaults to ``logging.getLogger("websockets.client")``.
  665. See the :doc:`logging guide <../../topics/logging>` for details.
  666. legacy: Set to :obj:`True` to opt into the historical behavior of
  667. returning a :class:`ClientConnection`, without deprecation warning.
  668. create_connection: Factory for the :class:`ClientConnection` managing
  669. the connection. Set it to a wrapper or a subclass to customize
  670. connection handling.
  671. Any other keyword arguments are passed to :func:`~socket.create_connection`.
  672. For example, you can set ``address`` to a ``(host, port)`` tuple to connect
  673. to a different host and port from those found in ``uri``. This only changes
  674. the destination of the TCP connection. The host name from ``uri`` is still
  675. used in the TLS handshake for secure connections and in the ``Host`` header.
  676. Raises:
  677. InvalidURI: If ``uri`` isn't a valid WebSocket URI.
  678. InvalidProxy: If ``proxy`` isn't a valid proxy.
  679. OSError: If the TCP connection fails.
  680. InvalidHandshake: If the opening handshake fails.
  681. TimeoutError: If the opening handshake times out.
  682. """
  683. connecter = reconnect(
  684. uri,
  685. sock=sock,
  686. ssl=ssl,
  687. server_hostname=server_hostname,
  688. origin=origin,
  689. extensions=extensions,
  690. subprotocols=subprotocols,
  691. compression=compression,
  692. additional_headers=additional_headers,
  693. user_agent_header=user_agent_header,
  694. proxy=proxy,
  695. proxy_ssl=proxy_ssl,
  696. proxy_server_hostname=proxy_server_hostname,
  697. open_timeout=open_timeout,
  698. ping_interval=ping_interval,
  699. ping_timeout=ping_timeout,
  700. close_timeout=close_timeout,
  701. max_size=max_size,
  702. max_queue=max_queue,
  703. logger=logger,
  704. create_connection=create_connection,
  705. **kwargs,
  706. )
  707. # For backwards compatibility, connect defaults to the historical behavior.
  708. # For forwards compatibility, the future behavior can be chosen explicitly.
  709. if legacy is False:
  710. return connecter
  711. connection = connecter.connect()
  712. # Users can opt in to the historical behavior to remain unaffected when the
  713. # future behavior becomes the default.
  714. if legacy:
  715. connection.pending_legacy_warning = False
  716. return connection
  717. def unix_reconnect(
  718. path: PathLike | None = None,
  719. uri: str | None = None,
  720. **kwargs: Any,
  721. ) -> reconnect:
  722. """
  723. Similar to :func:`unix_connect`, with support for automatic reconnection.
  724. Refer to the documentation of :func:`reconnect` for details on its behavior.
  725. """
  726. sock = kwargs.get("sock")
  727. if path is None and sock is None:
  728. raise ValueError("missing path argument")
  729. elif path is not None and sock is not None:
  730. raise ValueError("path is incompatible with sock")
  731. if uri is None:
  732. # Backwards compatibility: ssl used to be called ssl_context.
  733. if kwargs.get("ssl") is None and kwargs.get("ssl_context") is None:
  734. uri = "ws://localhost/"
  735. else:
  736. uri = "wss://localhost/"
  737. return reconnect(uri=uri, unix=True, path=path, **kwargs)
  738. @overload
  739. def unix_connect(
  740. path: PathLike | None = ...,
  741. uri: str | None = ...,
  742. *,
  743. legacy: Literal[True] | None = ...,
  744. **kwargs: Any,
  745. ) -> ClientConnection: ...
  746. @overload
  747. def unix_connect(
  748. path: PathLike | None = ...,
  749. uri: str | None = ...,
  750. *,
  751. legacy: Literal[False],
  752. **kwargs: Any,
  753. ) -> reconnect: ...
  754. def unix_connect(
  755. path: PathLike | None = None,
  756. uri: str | None = None,
  757. *,
  758. legacy: bool | None = None,
  759. **kwargs: Any,
  760. ) -> ClientConnection | reconnect:
  761. """
  762. Connect to a WebSocket server listening on a Unix socket.
  763. This function accepts the same keyword arguments as :func:`connect`.
  764. It's only available on Unix.
  765. It's mainly useful for debugging servers listening on Unix sockets.
  766. Args:
  767. path: File system path to the Unix socket.
  768. uri: URI of the WebSocket server. ``uri`` defaults to
  769. ``ws://localhost/`` or, when a ``ssl`` is provided, to
  770. ``wss://localhost/``.
  771. """
  772. connecter = unix_reconnect(path, uri, **kwargs)
  773. # For backwards compatibility, connect defaults to the historical behavior.
  774. # For forwards compatibility, the future behavior can be chosen explicitly.
  775. if legacy is False:
  776. return connecter
  777. connection = connecter.connect()
  778. # Users can opt in to the historical behavior to remain unaffected when the
  779. # future behavior becomes the default.
  780. if legacy:
  781. connection.pending_legacy_warning = False
  782. return connection
  783. try:
  784. from python_socks import ProxyType
  785. from python_socks.sync import Proxy as SocksProxy
  786. except ImportError:
  787. def connect_socks_proxy(
  788. proxy: Proxy,
  789. ws_uri: WebSocketURI,
  790. deadline: Deadline,
  791. **kwargs: Any,
  792. ) -> socket.socket:
  793. raise ImportError("connecting through a SOCKS proxy requires python-socks")
  794. else:
  795. SOCKS_PROXY_TYPES = {
  796. "socks5h": ProxyType.SOCKS5,
  797. "socks5": ProxyType.SOCKS5,
  798. "socks4a": ProxyType.SOCKS4,
  799. "socks4": ProxyType.SOCKS4,
  800. }
  801. SOCKS_PROXY_RDNS = {
  802. "socks5h": True,
  803. "socks5": False,
  804. "socks4a": True,
  805. "socks4": False,
  806. }
  807. def connect_socks_proxy(
  808. proxy: Proxy,
  809. ws_uri: WebSocketURI,
  810. deadline: Deadline,
  811. **kwargs: Any,
  812. ) -> socket.socket:
  813. """Connect via a SOCKS proxy and return the socket."""
  814. socks_proxy = SocksProxy(
  815. SOCKS_PROXY_TYPES[proxy.scheme],
  816. proxy.host,
  817. proxy.port,
  818. proxy.username,
  819. proxy.password,
  820. SOCKS_PROXY_RDNS[proxy.scheme],
  821. )
  822. kwargs.setdefault("timeout", deadline.timeout())
  823. # connect() is documented to raise OSError and TimeoutError.
  824. # Wrap other exceptions in ProxyError, a subclass of InvalidHandshake.
  825. try:
  826. return socks_proxy.connect(ws_uri.host, ws_uri.port, **kwargs)
  827. except (OSError, TimeoutError, socket.timeout):
  828. raise
  829. except Exception as exc:
  830. raise ProxyError("failed to connect to SOCKS proxy") from exc
  831. def read_connect_response(sock: socket.socket, deadline: Deadline) -> Response:
  832. reader = StreamReader()
  833. parser = Response.parse(
  834. reader.read_line,
  835. reader.read_exact,
  836. reader.read_to_eof,
  837. proxy=True,
  838. )
  839. try:
  840. while True:
  841. sock.settimeout(deadline.timeout())
  842. data = sock.recv(4096)
  843. if data:
  844. reader.feed_data(data)
  845. else:
  846. reader.feed_eof()
  847. next(parser)
  848. except StopIteration as exc:
  849. assert isinstance(exc.value, Response) # help mypy
  850. response = exc.value
  851. if 200 <= response.status_code < 300:
  852. return response
  853. else:
  854. raise InvalidProxyStatus(response)
  855. except socket.timeout:
  856. raise TimeoutError("timed out while connecting to HTTP proxy")
  857. except Exception as exc:
  858. raise InvalidProxyMessage(
  859. "did not receive a valid HTTP response from proxy"
  860. ) from exc
  861. finally:
  862. sock.settimeout(None)
  863. def connect_http_proxy(
  864. proxy: Proxy,
  865. ws_uri: WebSocketURI,
  866. deadline: Deadline,
  867. *,
  868. user_agent_header: str | None = None,
  869. ssl: ssl_module.SSLContext | None = None,
  870. server_hostname: str | None = None,
  871. **kwargs: Any,
  872. ) -> socket.socket:
  873. # Connect socket
  874. kwargs.setdefault("timeout", deadline.timeout())
  875. sock = socket.create_connection((proxy.host, proxy.port), **kwargs)
  876. # Initialize TLS wrapper and perform TLS handshake
  877. if proxy.scheme == "https":
  878. if ssl is None:
  879. ssl = ssl_module.create_default_context()
  880. if server_hostname is None:
  881. server_hostname = proxy.host
  882. sock.settimeout(deadline.timeout())
  883. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  884. sock.settimeout(None)
  885. # Send CONNECT request to the proxy and read response.
  886. request = prepare_connect_request(proxy, ws_uri, user_agent_header)
  887. sock.sendall(request)
  888. try:
  889. read_connect_response(sock, deadline)
  890. except Exception:
  891. sock.close()
  892. raise
  893. return sock
  894. T = TypeVar("T")
  895. F = TypeVar("F", bound=Callable[..., T])
  896. class SSLSSLSocket:
  897. """
  898. Socket-like object providing TLS-in-TLS.
  899. Only methods that are used by websockets are implemented.
  900. """
  901. recv_bufsize = 65536
  902. def __init__(
  903. self,
  904. sock: socket.socket,
  905. ssl_context: ssl_module.SSLContext,
  906. server_hostname: str | None = None,
  907. ) -> None:
  908. self.incoming = ssl_module.MemoryBIO()
  909. self.outgoing = ssl_module.MemoryBIO()
  910. self.ssl_socket = sock
  911. self.ssl_object = ssl_context.wrap_bio(
  912. self.incoming,
  913. self.outgoing,
  914. server_hostname=server_hostname,
  915. )
  916. self.run_io(self.ssl_object.do_handshake)
  917. def run_io(self, func: Callable[..., T], *args: Any) -> T:
  918. while True:
  919. want_read = False
  920. want_write = False
  921. try:
  922. result = func(*args)
  923. except ssl_module.SSLWantReadError:
  924. want_read = True
  925. except ssl_module.SSLWantWriteError: # pragma: no cover
  926. want_write = True
  927. # Write outgoing data in all cases.
  928. data = self.outgoing.read()
  929. if data:
  930. self.ssl_socket.sendall(data)
  931. # Read incoming data and retry on SSLWantReadError.
  932. if want_read:
  933. data = self.ssl_socket.recv(self.recv_bufsize)
  934. if data:
  935. self.incoming.write(data)
  936. else:
  937. self.incoming.write_eof()
  938. continue
  939. # Retry after writing outgoing data on SSLWantWriteError.
  940. if want_write: # pragma: no cover
  941. continue
  942. # Return result if no error happened.
  943. return result
  944. def recv(self, buflen: int) -> bytes:
  945. try:
  946. return self.run_io(self.ssl_object.read, buflen)
  947. except ssl_module.SSLEOFError:
  948. return b"" # always ignore ragged EOFs
  949. def send(self, data: BytesLike) -> int:
  950. return self.run_io(self.ssl_object.write, data)
  951. def sendall(self, data: BytesLike) -> None:
  952. # adapted from ssl_module.SSLSocket.sendall()
  953. count = 0
  954. with memoryview(data) as view, view.cast("B") as byte_view:
  955. amount = len(byte_view)
  956. while count < amount:
  957. count += self.send(byte_view[count:])
  958. # recv_into(), recvfrom(), recvfrom_into(), sendto(), unwrap(), and the
  959. # flags argument aren't implemented because websockets doesn't need them.
  960. def __getattr__(self, name: str) -> Any:
  961. return getattr(self.ssl_socket, name)