You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1238 lines
48 KiB

  1. from __future__ import annotations
  2. import asyncio
  3. import collections
  4. import contextlib
  5. import logging
  6. import random
  7. import struct
  8. import sys
  9. import traceback
  10. import uuid
  11. from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Iterable, Mapping
  12. from types import TracebackType
  13. from typing import Any, Literal, cast, overload
  14. from ..exceptions import (
  15. ConcurrencyError,
  16. ConnectionClosed,
  17. ConnectionClosedOK,
  18. ProtocolError,
  19. )
  20. from ..frames import DATA_OPCODES, BytesLike, CloseCode, Frame, Opcode
  21. from ..http11 import Request, Response
  22. from ..protocol import CLOSED, OPEN, Event, Protocol, State
  23. from ..typing import Data, LoggerLike, Subprotocol
  24. from .compatibility import (
  25. TimeoutError,
  26. aiter,
  27. anext,
  28. asyncio_timeout,
  29. asyncio_timeout_at,
  30. )
  31. from .messages import Assembler
  32. __all__ = ["Connection"]
  33. class Connection(asyncio.Protocol):
  34. """
  35. :mod:`asyncio` implementation of a WebSocket connection.
  36. :class:`Connection` provides APIs shared between WebSocket servers and
  37. clients.
  38. You shouldn't use it directly. Instead, use
  39. :class:`~websockets.asyncio.client.ClientConnection` or
  40. :class:`~websockets.asyncio.server.ServerConnection`.
  41. """
  42. def __init__(
  43. self,
  44. protocol: Protocol,
  45. *,
  46. ping_interval: float | None = 20,
  47. ping_timeout: float | None = 20,
  48. close_timeout: float | None = 10,
  49. max_queue: int | None | tuple[int | None, int | None] = 16,
  50. write_limit: int | tuple[int, int | None] = 2**15,
  51. ) -> None:
  52. self.protocol = protocol
  53. self.ping_interval = ping_interval
  54. self.ping_timeout = ping_timeout
  55. self.close_timeout = close_timeout
  56. if isinstance(max_queue, int) or max_queue is None:
  57. max_queue = (max_queue, None)
  58. self.max_queue = max_queue
  59. if isinstance(write_limit, int):
  60. write_limit = (write_limit, None)
  61. self.write_limit = write_limit
  62. # Inject reference to this instance in the protocol's logger.
  63. self.protocol.logger = logging.LoggerAdapter(
  64. self.protocol.logger,
  65. {"websocket": self},
  66. )
  67. # Copy attributes from the protocol for convenience.
  68. self.id: uuid.UUID = self.protocol.id
  69. """Unique identifier of the connection. Useful in logs."""
  70. self.logger: LoggerLike = self.protocol.logger
  71. """Logger for this connection."""
  72. self.debug = self.protocol.debug
  73. # HTTP handshake request and response.
  74. self.request: Request | None = None
  75. """Opening handshake request."""
  76. self.response: Response | None = None
  77. """Opening handshake response."""
  78. # Event loop running this connection.
  79. self.loop = asyncio.get_running_loop()
  80. # Assembler turning frames into messages and serializing reads.
  81. self.recv_messages: Assembler # initialized in connection_made
  82. # Deadline for the closing handshake.
  83. self.close_deadline: float | None = None
  84. # Protect sending fragmented messages.
  85. self.fragmented_send_waiter: asyncio.Future[None] | None = None
  86. # Mapping of ping IDs to pong waiters, in chronological order.
  87. self.pong_waiters: dict[bytes, tuple[asyncio.Future[float], float]] = {}
  88. self.latency: float = 0
  89. """
  90. Latency of the connection, in seconds.
  91. Latency is defined as the round-trip time of the connection. It is
  92. measured by sending a Ping frame and waiting for a matching Pong frame.
  93. Before the first measurement, :attr:`latency` is ``0``.
  94. By default, websockets enables a :ref:`keepalive <keepalive>` mechanism
  95. that sends Ping frames automatically at regular intervals. You can also
  96. send Ping frames and measure latency with :meth:`ping`.
  97. """
  98. # Task that sends keepalive pings. None when ping_interval is None.
  99. self.keepalive_task: asyncio.Task[None] | None = None
  100. # Exception raised while reading from the connection, to be chained to
  101. # ConnectionClosed in order to show why the TCP connection dropped.
  102. self.recv_exc: BaseException | None = None
  103. # Completed when the TCP connection is closed and the WebSocket
  104. # connection state becomes CLOSED.
  105. self.connection_lost_waiter: asyncio.Future[None] = self.loop.create_future()
  106. # Adapted from asyncio.FlowControlMixin
  107. self.paused: bool = False
  108. self.drain_waiters: collections.deque[asyncio.Future[None]] = (
  109. collections.deque()
  110. )
  111. # Public attributes
  112. @property
  113. def local_address(self) -> Any:
  114. """
  115. Local address of the connection.
  116. For IPv4 connections, this is a ``(host, port)`` tuple.
  117. The format of the address depends on the address family.
  118. See :meth:`~socket.socket.getsockname`.
  119. """
  120. return self.transport.get_extra_info("sockname")
  121. @property
  122. def remote_address(self) -> Any:
  123. """
  124. Remote address of the connection.
  125. For IPv4 connections, this is a ``(host, port)`` tuple.
  126. The format of the address depends on the address family.
  127. See :meth:`~socket.socket.getpeername`.
  128. """
  129. return self.transport.get_extra_info("peername")
  130. @property
  131. def state(self) -> State:
  132. """
  133. State of the WebSocket connection, defined in :rfc:`6455`.
  134. This attribute is provided for completeness. Typical applications
  135. shouldn't check its value. Instead, they should call :meth:`~recv` or
  136. :meth:`send` and handle :exc:`~websockets.exceptions.ConnectionClosed`
  137. exceptions.
  138. """
  139. return self.protocol.state
  140. @property
  141. def subprotocol(self) -> Subprotocol | None:
  142. """
  143. Subprotocol negotiated during the opening handshake.
  144. :obj:`None` if no subprotocol was negotiated.
  145. """
  146. return self.protocol.subprotocol
  147. @property
  148. def close_code(self) -> int | None:
  149. """
  150. State of the WebSocket connection, defined in :rfc:`6455`.
  151. This attribute is provided for completeness. Typical applications
  152. shouldn't check its value. Instead, they should inspect attributes
  153. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  154. """
  155. return self.protocol.close_code
  156. @property
  157. def close_reason(self) -> str | None:
  158. """
  159. State of the WebSocket connection, defined in :rfc:`6455`.
  160. This attribute is provided for completeness. Typical applications
  161. shouldn't check its value. Instead, they should inspect attributes
  162. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  163. """
  164. return self.protocol.close_reason
  165. # Public methods
  166. async def __aenter__(self) -> Connection:
  167. return self
  168. async def __aexit__(
  169. self,
  170. exc_type: type[BaseException] | None,
  171. exc_value: BaseException | None,
  172. traceback: TracebackType | None,
  173. ) -> None:
  174. if exc_type is None:
  175. await self.close()
  176. else:
  177. await self.close(CloseCode.INTERNAL_ERROR)
  178. async def __aiter__(self) -> AsyncIterator[Data]:
  179. """
  180. Iterate on incoming messages.
  181. The iterator calls :meth:`recv` and yields messages asynchronously in an
  182. infinite loop.
  183. It exits when the connection is closed normally. It raises a
  184. :exc:`~websockets.exceptions.ConnectionClosedError` exception after a
  185. protocol error or a network failure.
  186. """
  187. try:
  188. while True:
  189. yield await self.recv()
  190. except ConnectionClosedOK:
  191. return
  192. @overload
  193. async def recv(self, decode: Literal[True]) -> str: ...
  194. @overload
  195. async def recv(self, decode: Literal[False]) -> bytes: ...
  196. @overload
  197. async def recv(self, decode: bool | None = None) -> Data: ...
  198. async def recv(self, decode: bool | None = None) -> Data:
  199. """
  200. Receive the next message.
  201. When the connection is closed, :meth:`recv` raises
  202. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises
  203. :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal closure
  204. and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  205. error or a network failure. This is how you detect the end of the
  206. message stream.
  207. Canceling :meth:`recv` is safe. There's no risk of losing data. The next
  208. invocation of :meth:`recv` will return the next message.
  209. This makes it possible to enforce a timeout by wrapping :meth:`recv` in
  210. :func:`~asyncio.timeout` or :func:`~asyncio.wait_for`.
  211. When the message is fragmented, :meth:`recv` waits until all fragments
  212. are received, reassembles them, and returns the whole message.
  213. Args:
  214. decode: Set this flag to override the default behavior of returning
  215. :class:`str` or :class:`bytes`. See below for details.
  216. Returns:
  217. A string (:class:`str`) for a Text_ frame or a bytestring
  218. (:class:`bytes`) for a Binary_ frame.
  219. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  220. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  221. You may override this behavior with the ``decode`` argument:
  222. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames and
  223. return a bytestring (:class:`bytes`). This improves performance
  224. when decoding isn't needed, for example if the message contains
  225. JSON and you're using a JSON library that expects a bytestring.
  226. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
  227. and return a string (:class:`str`). This may be useful for
  228. servers that send binary frames instead of text frames.
  229. Raises:
  230. ConnectionClosed: When the connection is closed.
  231. ConcurrencyError: If two coroutines call :meth:`recv` or
  232. :meth:`recv_streaming` concurrently.
  233. """
  234. try:
  235. return await self.recv_messages.get(decode)
  236. except EOFError:
  237. pass
  238. # fallthrough
  239. except ConcurrencyError:
  240. raise ConcurrencyError(
  241. "cannot call recv while another coroutine "
  242. "is already running recv or recv_streaming"
  243. ) from None
  244. except UnicodeDecodeError as exc:
  245. async with self.send_context():
  246. self.protocol.fail(
  247. CloseCode.INVALID_DATA,
  248. f"{exc.reason} at position {exc.start}",
  249. )
  250. # fallthrough
  251. # Wait for the protocol state to be CLOSED before accessing close_exc.
  252. await asyncio.shield(self.connection_lost_waiter)
  253. raise self.protocol.close_exc from self.recv_exc
  254. @overload
  255. def recv_streaming(self, decode: Literal[True]) -> AsyncIterator[str]: ...
  256. @overload
  257. def recv_streaming(self, decode: Literal[False]) -> AsyncIterator[bytes]: ...
  258. @overload
  259. def recv_streaming(self, decode: bool | None = None) -> AsyncIterator[Data]: ...
  260. async def recv_streaming(self, decode: bool | None = None) -> AsyncIterator[Data]:
  261. """
  262. Receive the next message frame by frame.
  263. This method is designed for receiving fragmented messages. It returns an
  264. asynchronous iterator that yields each fragment as it is received. This
  265. iterator must be fully consumed. Else, future calls to :meth:`recv` or
  266. :meth:`recv_streaming` will raise
  267. :exc:`~websockets.exceptions.ConcurrencyError`, making the connection
  268. unusable.
  269. :meth:`recv_streaming` raises the same exceptions as :meth:`recv`.
  270. Canceling :meth:`recv_streaming` before receiving the first frame is
  271. safe. Canceling it after receiving one or more frames leaves the
  272. iterator in a partially consumed state, making the connection unusable.
  273. Instead, you should close the connection with :meth:`close`.
  274. Args:
  275. decode: Set this flag to override the default behavior of returning
  276. :class:`str` or :class:`bytes`. See below for details.
  277. Returns:
  278. An iterator of strings (:class:`str`) for a Text_ frame or
  279. bytestrings (:class:`bytes`) for a Binary_ frame.
  280. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  281. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  282. You may override this behavior with the ``decode`` argument:
  283. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames
  284. and return bytestrings (:class:`bytes`). This may be useful to
  285. optimize performance when decoding isn't needed.
  286. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
  287. and return strings (:class:`str`). This is useful for servers
  288. that send binary frames instead of text frames.
  289. Raises:
  290. ConnectionClosed: When the connection is closed.
  291. ConcurrencyError: If two coroutines call :meth:`recv` or
  292. :meth:`recv_streaming` concurrently.
  293. """
  294. try:
  295. async for frame in self.recv_messages.get_iter(decode):
  296. yield frame
  297. return
  298. except EOFError:
  299. pass
  300. # fallthrough
  301. except ConcurrencyError:
  302. raise ConcurrencyError(
  303. "cannot call recv_streaming while another coroutine "
  304. "is already running recv or recv_streaming"
  305. ) from None
  306. except UnicodeDecodeError as exc:
  307. async with self.send_context():
  308. self.protocol.fail(
  309. CloseCode.INVALID_DATA,
  310. f"{exc.reason} at position {exc.start}",
  311. )
  312. # fallthrough
  313. # Wait for the protocol state to be CLOSED before accessing close_exc.
  314. await asyncio.shield(self.connection_lost_waiter)
  315. raise self.protocol.close_exc from self.recv_exc
  316. async def send(
  317. self,
  318. message: Data | Iterable[Data] | AsyncIterable[Data],
  319. text: bool | None = None,
  320. ) -> None:
  321. """
  322. Send a message.
  323. A string (:class:`str`) is sent as a Text_ frame. A bytestring or
  324. bytes-like object (:class:`bytes`, :class:`bytearray`, or
  325. :class:`memoryview`) is sent as a Binary_ frame.
  326. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  327. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  328. You may override this behavior with the ``text`` argument:
  329. * Set ``text=True`` to send a bytestring or bytes-like object
  330. (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) as a
  331. Text_ frame. This improves performance when the message is already
  332. UTF-8 encoded, for example if the message contains JSON and you're
  333. using a JSON library that produces a bytestring.
  334. * Set ``text=False`` to send a string (:class:`str`) in a Binary_
  335. frame. This may be useful for servers that expect binary frames
  336. instead of text frames.
  337. :meth:`send` also accepts an iterable or an asynchronous iterable of
  338. strings, bytestrings, or bytes-like objects to enable fragmentation_.
  339. Each item is treated as a message fragment and sent in its own frame.
  340. All items must be of the same type, or else :meth:`send` will raise a
  341. :exc:`TypeError` and the connection will be closed.
  342. .. _fragmentation: https://datatracker.ietf.org/doc/html/rfc6455#section-5.4
  343. :meth:`send` rejects dict-like objects because this is often an error.
  344. (If you really want to send the keys of a dict-like object as fragments,
  345. call its :meth:`~dict.keys` method and pass the result to :meth:`send`.)
  346. Canceling :meth:`send` is discouraged. Instead, you should close the
  347. connection with :meth:`close`. Indeed, there are only two situations
  348. where :meth:`send` may yield control to the event loop and then get
  349. canceled; in both cases, :meth:`close` has the same effect and is
  350. more clear:
  351. 1. The write buffer is full. If you don't want to wait until enough
  352. data is sent, your only alternative is to close the connection.
  353. :meth:`close` will likely time out then abort the TCP connection.
  354. 2. ``message`` is an asynchronous iterator that yields control.
  355. Stopping in the middle of a fragmented message will cause a
  356. protocol error and the connection will be closed.
  357. When the connection is closed, :meth:`send` raises
  358. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it
  359. raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal
  360. connection closure and
  361. :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  362. error or a network failure.
  363. Args:
  364. message: Message to send.
  365. Raises:
  366. ConnectionClosed: When the connection is closed.
  367. TypeError: If ``message`` doesn't have a supported type.
  368. """
  369. # While sending a fragmented message, prevent sending other messages
  370. # until all fragments are sent.
  371. while self.fragmented_send_waiter is not None:
  372. await asyncio.shield(self.fragmented_send_waiter)
  373. # Unfragmented message -- this case must be handled first because
  374. # strings and bytes-like objects are iterable.
  375. if isinstance(message, str):
  376. async with self.send_context():
  377. if text is False:
  378. self.protocol.send_binary(message.encode())
  379. else:
  380. self.protocol.send_text(message.encode())
  381. elif isinstance(message, BytesLike):
  382. async with self.send_context():
  383. if text is True:
  384. self.protocol.send_text(message)
  385. else:
  386. self.protocol.send_binary(message)
  387. # Catch a common mistake -- passing a dict to send().
  388. elif isinstance(message, Mapping):
  389. raise TypeError("data is a dict-like object")
  390. # Fragmented message -- regular iterator.
  391. elif isinstance(message, Iterable):
  392. chunks = iter(message)
  393. try:
  394. chunk = next(chunks)
  395. except StopIteration:
  396. return
  397. assert self.fragmented_send_waiter is None
  398. self.fragmented_send_waiter = self.loop.create_future()
  399. try:
  400. # First fragment.
  401. if isinstance(chunk, str):
  402. async with self.send_context():
  403. if text is False:
  404. self.protocol.send_binary(chunk.encode(), fin=False)
  405. else:
  406. self.protocol.send_text(chunk.encode(), fin=False)
  407. encode = True
  408. elif isinstance(chunk, BytesLike):
  409. async with self.send_context():
  410. if text is True:
  411. self.protocol.send_text(chunk, fin=False)
  412. else:
  413. self.protocol.send_binary(chunk, fin=False)
  414. encode = False
  415. else:
  416. raise TypeError("iterable must contain bytes or str")
  417. # Other fragments
  418. for chunk in chunks:
  419. if isinstance(chunk, str) and encode:
  420. async with self.send_context():
  421. self.protocol.send_continuation(chunk.encode(), fin=False)
  422. elif isinstance(chunk, BytesLike) and not encode:
  423. async with self.send_context():
  424. self.protocol.send_continuation(chunk, fin=False)
  425. else:
  426. raise TypeError("iterable must contain uniform types")
  427. # Final fragment.
  428. async with self.send_context():
  429. self.protocol.send_continuation(b"", fin=True)
  430. except Exception:
  431. # We're half-way through a fragmented message and we can't
  432. # complete it. This makes the connection unusable.
  433. async with self.send_context():
  434. self.protocol.fail(
  435. CloseCode.INTERNAL_ERROR,
  436. "error in fragmented message",
  437. )
  438. raise
  439. finally:
  440. self.fragmented_send_waiter.set_result(None)
  441. self.fragmented_send_waiter = None
  442. # Fragmented message -- async iterator.
  443. elif isinstance(message, AsyncIterable):
  444. achunks = aiter(message)
  445. try:
  446. chunk = await anext(achunks)
  447. except StopAsyncIteration:
  448. return
  449. assert self.fragmented_send_waiter is None
  450. self.fragmented_send_waiter = self.loop.create_future()
  451. try:
  452. # First fragment.
  453. if isinstance(chunk, str):
  454. if text is False:
  455. async with self.send_context():
  456. self.protocol.send_binary(chunk.encode(), fin=False)
  457. else:
  458. async with self.send_context():
  459. self.protocol.send_text(chunk.encode(), fin=False)
  460. encode = True
  461. elif isinstance(chunk, BytesLike):
  462. if text is True:
  463. async with self.send_context():
  464. self.protocol.send_text(chunk, fin=False)
  465. else:
  466. async with self.send_context():
  467. self.protocol.send_binary(chunk, fin=False)
  468. encode = False
  469. else:
  470. raise TypeError("async iterable must contain bytes or str")
  471. # Other fragments
  472. async for chunk in achunks:
  473. if isinstance(chunk, str) and encode:
  474. async with self.send_context():
  475. self.protocol.send_continuation(chunk.encode(), fin=False)
  476. elif isinstance(chunk, BytesLike) and not encode:
  477. async with self.send_context():
  478. self.protocol.send_continuation(chunk, fin=False)
  479. else:
  480. raise TypeError("async iterable must contain uniform types")
  481. # Final fragment.
  482. async with self.send_context():
  483. self.protocol.send_continuation(b"", fin=True)
  484. except Exception:
  485. # We're half-way through a fragmented message and we can't
  486. # complete it. This makes the connection unusable.
  487. async with self.send_context():
  488. self.protocol.fail(
  489. CloseCode.INTERNAL_ERROR,
  490. "error in fragmented message",
  491. )
  492. raise
  493. finally:
  494. self.fragmented_send_waiter.set_result(None)
  495. self.fragmented_send_waiter = None
  496. else:
  497. raise TypeError("data must be str, bytes, iterable, or async iterable")
  498. async def close(self, code: int = 1000, reason: str = "") -> None:
  499. """
  500. Perform the closing handshake.
  501. :meth:`close` waits for the other end to complete the handshake and
  502. for the TCP connection to terminate.
  503. :meth:`close` is idempotent: it doesn't do anything once the
  504. connection is closed.
  505. Args:
  506. code: WebSocket close code.
  507. reason: WebSocket close reason.
  508. """
  509. try:
  510. # The context manager takes care of waiting for the TCP connection
  511. # to terminate after calling a method that sends a close frame.
  512. async with self.send_context():
  513. if self.fragmented_send_waiter is not None:
  514. self.protocol.fail(
  515. CloseCode.INTERNAL_ERROR,
  516. "close during fragmented message",
  517. )
  518. else:
  519. self.protocol.send_close(code, reason)
  520. except ConnectionClosed:
  521. # Ignore ConnectionClosed exceptions raised from send_context().
  522. # They mean that the connection is closed, which was the goal.
  523. pass
  524. async def wait_closed(self) -> None:
  525. """
  526. Wait until the connection is closed.
  527. :meth:`wait_closed` waits for the closing handshake to complete and for
  528. the TCP connection to terminate.
  529. """
  530. await asyncio.shield(self.connection_lost_waiter)
  531. async def ping(self, data: Data | None = None) -> Awaitable[float]:
  532. """
  533. Send a Ping_.
  534. .. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2
  535. A ping may serve as a keepalive or as a check that the remote endpoint
  536. received all messages up to this point
  537. Args:
  538. data: Payload of the ping. A :class:`str` will be encoded to UTF-8.
  539. If ``data`` is :obj:`None`, the payload is four random bytes.
  540. Returns:
  541. A future that will be completed when the corresponding pong is
  542. received. You can ignore it if you don't intend to wait. The result
  543. of the future is the latency of the connection in seconds.
  544. ::
  545. pong_waiter = await ws.ping()
  546. # only if you want to wait for the corresponding pong
  547. latency = await pong_waiter
  548. Raises:
  549. ConnectionClosed: When the connection is closed.
  550. ConcurrencyError: If another ping was sent with the same data and
  551. the corresponding pong wasn't received yet.
  552. """
  553. if isinstance(data, BytesLike):
  554. data = bytes(data)
  555. elif isinstance(data, str):
  556. data = data.encode()
  557. elif data is not None:
  558. raise TypeError("data must be str or bytes-like")
  559. async with self.send_context():
  560. # Protect against duplicates if a payload is explicitly set.
  561. if data in self.pong_waiters:
  562. raise ConcurrencyError("already waiting for a pong with the same data")
  563. # Generate a unique random payload otherwise.
  564. while data is None or data in self.pong_waiters:
  565. data = struct.pack("!I", random.getrandbits(32))
  566. pong_waiter = self.loop.create_future()
  567. # The event loop's default clock is time.monotonic(). Its resolution
  568. # is a bit low on Windows (~16ms). This is improved in Python 3.13.
  569. self.pong_waiters[data] = (pong_waiter, self.loop.time())
  570. self.protocol.send_ping(data)
  571. return pong_waiter
  572. async def pong(self, data: Data = b"") -> None:
  573. """
  574. Send a Pong_.
  575. .. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3
  576. An unsolicited pong may serve as a unidirectional heartbeat.
  577. Args:
  578. data: Payload of the pong. A :class:`str` will be encoded to UTF-8.
  579. Raises:
  580. ConnectionClosed: When the connection is closed.
  581. """
  582. if isinstance(data, BytesLike):
  583. data = bytes(data)
  584. elif isinstance(data, str):
  585. data = data.encode()
  586. else:
  587. raise TypeError("data must be str or bytes-like")
  588. async with self.send_context():
  589. self.protocol.send_pong(data)
  590. # Private methods
  591. def process_event(self, event: Event) -> None:
  592. """
  593. Process one incoming event.
  594. This method is overridden in subclasses to handle the handshake.
  595. """
  596. assert isinstance(event, Frame)
  597. if event.opcode in DATA_OPCODES:
  598. self.recv_messages.put(event)
  599. if event.opcode is Opcode.PONG:
  600. self.acknowledge_pings(bytes(event.data))
  601. def acknowledge_pings(self, data: bytes) -> None:
  602. """
  603. Acknowledge pings when receiving a pong.
  604. """
  605. # Ignore unsolicited pong.
  606. if data not in self.pong_waiters:
  607. return
  608. pong_timestamp = self.loop.time()
  609. # Sending a pong for only the most recent ping is legal.
  610. # Acknowledge all previous pings too in that case.
  611. ping_id = None
  612. ping_ids = []
  613. for ping_id, (pong_waiter, ping_timestamp) in self.pong_waiters.items():
  614. ping_ids.append(ping_id)
  615. latency = pong_timestamp - ping_timestamp
  616. if not pong_waiter.done():
  617. pong_waiter.set_result(latency)
  618. if ping_id == data:
  619. self.latency = latency
  620. break
  621. else:
  622. raise AssertionError("solicited pong not found in pings")
  623. # Remove acknowledged pings from self.pong_waiters.
  624. for ping_id in ping_ids:
  625. del self.pong_waiters[ping_id]
  626. def abort_pings(self) -> None:
  627. """
  628. Raise ConnectionClosed in pending pings.
  629. They'll never receive a pong once the connection is closed.
  630. """
  631. assert self.protocol.state is CLOSED
  632. exc = self.protocol.close_exc
  633. for pong_waiter, _ping_timestamp in self.pong_waiters.values():
  634. if not pong_waiter.done():
  635. pong_waiter.set_exception(exc)
  636. # If the exception is never retrieved, it will be logged when ping
  637. # is garbage-collected. This is confusing for users.
  638. # Given that ping is done (with an exception), canceling it does
  639. # nothing, but it prevents logging the exception.
  640. pong_waiter.cancel()
  641. self.pong_waiters.clear()
  642. async def keepalive(self) -> None:
  643. """
  644. Send a Ping frame and wait for a Pong frame at regular intervals.
  645. """
  646. assert self.ping_interval is not None
  647. latency = 0.0
  648. try:
  649. while True:
  650. # If self.ping_timeout > latency > self.ping_interval,
  651. # pings will be sent immediately after receiving pongs.
  652. # The period will be longer than self.ping_interval.
  653. await asyncio.sleep(self.ping_interval - latency)
  654. # This cannot raise ConnectionClosed when the connection is
  655. # closing because ping(), via send_context(), waits for the
  656. # connection to be closed before raising ConnectionClosed.
  657. # However, connection_lost() cancels keepalive_task before
  658. # it gets a chance to resume excuting.
  659. pong_waiter = await self.ping()
  660. if self.debug:
  661. self.logger.debug("% sent keepalive ping")
  662. if self.ping_timeout is not None:
  663. try:
  664. async with asyncio_timeout(self.ping_timeout):
  665. # connection_lost cancels keepalive immediately
  666. # after setting a ConnectionClosed exception on
  667. # pong_waiter. A CancelledError is raised here,
  668. # not a ConnectionClosed exception.
  669. latency = await pong_waiter
  670. self.logger.debug("% received keepalive pong")
  671. except asyncio.TimeoutError:
  672. if self.debug:
  673. self.logger.debug("- timed out waiting for keepalive pong")
  674. async with self.send_context():
  675. self.protocol.fail(
  676. CloseCode.INTERNAL_ERROR,
  677. "keepalive ping timeout",
  678. )
  679. raise AssertionError(
  680. "send_context() should wait for connection_lost(), "
  681. "which cancels keepalive()"
  682. )
  683. except Exception:
  684. self.logger.error("keepalive ping failed", exc_info=True)
  685. def start_keepalive(self) -> None:
  686. """
  687. Run :meth:`keepalive` in a task, unless keepalive is disabled.
  688. """
  689. if self.ping_interval is not None:
  690. self.keepalive_task = self.loop.create_task(self.keepalive())
  691. @contextlib.asynccontextmanager
  692. async def send_context(
  693. self,
  694. *,
  695. expected_state: State = OPEN, # CONNECTING during the opening handshake
  696. ) -> AsyncIterator[None]:
  697. """
  698. Create a context for writing to the connection from user code.
  699. On entry, :meth:`send_context` checks that the connection is open; on
  700. exit, it writes outgoing data to the socket::
  701. async with self.send_context():
  702. self.protocol.send_text(message.encode())
  703. When the connection isn't open on entry, when the connection is expected
  704. to close on exit, or when an unexpected error happens, terminating the
  705. connection, :meth:`send_context` waits until the connection is closed
  706. then raises :exc:`~websockets.exceptions.ConnectionClosed`.
  707. """
  708. # Should we wait until the connection is closed?
  709. wait_for_close = False
  710. # Should we close the transport and raise ConnectionClosed?
  711. raise_close_exc = False
  712. # What exception should we chain ConnectionClosed to?
  713. original_exc: BaseException | None = None
  714. if self.protocol.state is expected_state:
  715. # Let the caller interact with the protocol.
  716. try:
  717. yield
  718. except (ProtocolError, ConcurrencyError):
  719. # The protocol state wasn't changed. Exit immediately.
  720. raise
  721. except Exception as exc:
  722. self.logger.error("unexpected internal error", exc_info=True)
  723. # This branch should never run. It's a safety net in case of
  724. # bugs. Since we don't know what happened, we will close the
  725. # connection and raise the exception to the caller.
  726. wait_for_close = False
  727. raise_close_exc = True
  728. original_exc = exc
  729. else:
  730. # Check if the connection is expected to close soon.
  731. if self.protocol.close_expected():
  732. wait_for_close = True
  733. # If the connection is expected to close soon, set the
  734. # close deadline based on the close timeout.
  735. # Since we tested earlier that protocol.state was OPEN
  736. # (or CONNECTING), self.close_deadline is still None.
  737. if self.close_timeout is not None:
  738. assert self.close_deadline is None
  739. self.close_deadline = self.loop.time() + self.close_timeout
  740. # Write outgoing data to the socket and enforce flow control.
  741. try:
  742. self.send_data()
  743. await self.drain()
  744. except Exception as exc:
  745. if self.debug:
  746. self.logger.debug("! error while sending data", exc_info=True)
  747. # While the only expected exception here is OSError,
  748. # other exceptions would be treated identically.
  749. wait_for_close = False
  750. raise_close_exc = True
  751. original_exc = exc
  752. else: # self.protocol.state is not expected_state
  753. # Minor layering violation: we assume that the connection
  754. # will be closing soon if it isn't in the expected state.
  755. wait_for_close = True
  756. # Calculate close_deadline if it wasn't set yet.
  757. if self.close_timeout is not None:
  758. if self.close_deadline is None:
  759. self.close_deadline = self.loop.time() + self.close_timeout
  760. raise_close_exc = True
  761. # If the connection is expected to close soon and the close timeout
  762. # elapses, close the socket to terminate the connection.
  763. if wait_for_close:
  764. try:
  765. async with asyncio_timeout_at(self.close_deadline):
  766. await asyncio.shield(self.connection_lost_waiter)
  767. except TimeoutError:
  768. # There's no risk to overwrite another error because
  769. # original_exc is never set when wait_for_close is True.
  770. assert original_exc is None
  771. original_exc = TimeoutError("timed out while closing connection")
  772. # Set recv_exc before closing the transport in order to get
  773. # proper exception reporting.
  774. raise_close_exc = True
  775. self.set_recv_exc(original_exc)
  776. # If an error occurred, close the transport to terminate the connection and
  777. # raise an exception.
  778. if raise_close_exc:
  779. self.transport.abort()
  780. # Wait for the protocol state to be CLOSED before accessing close_exc.
  781. await asyncio.shield(self.connection_lost_waiter)
  782. raise self.protocol.close_exc from original_exc
  783. def send_data(self) -> None:
  784. """
  785. Send outgoing data.
  786. Raises:
  787. OSError: When a socket operations fails.
  788. """
  789. for data in self.protocol.data_to_send():
  790. if data:
  791. self.transport.write(data)
  792. else:
  793. # Half-close the TCP connection when possible i.e. no TLS.
  794. if self.transport.can_write_eof():
  795. if self.debug:
  796. self.logger.debug("x half-closing TCP connection")
  797. # write_eof() doesn't document which exceptions it raises.
  798. # OSError is plausible. uvloop can raise RuntimeError here.
  799. try:
  800. self.transport.write_eof()
  801. except (OSError, RuntimeError): # pragma: no cover
  802. pass
  803. # Else, close the TCP connection.
  804. else: # pragma: no cover
  805. if self.debug:
  806. self.logger.debug("x closing TCP connection")
  807. self.transport.close()
  808. def set_recv_exc(self, exc: BaseException | None) -> None:
  809. """
  810. Set recv_exc, if not set yet.
  811. """
  812. if self.recv_exc is None:
  813. self.recv_exc = exc
  814. # asyncio.Protocol methods
  815. # Connection callbacks
  816. def connection_made(self, transport: asyncio.BaseTransport) -> None:
  817. transport = cast(asyncio.Transport, transport)
  818. self.recv_messages = Assembler(
  819. *self.max_queue,
  820. pause=transport.pause_reading,
  821. resume=transport.resume_reading,
  822. )
  823. transport.set_write_buffer_limits(*self.write_limit)
  824. self.transport = transport
  825. def connection_lost(self, exc: Exception | None) -> None:
  826. # Calling protocol.receive_eof() is safe because it's idempotent.
  827. # This guarantees that the protocol state becomes CLOSED.
  828. self.protocol.receive_eof()
  829. assert self.protocol.state is CLOSED
  830. self.set_recv_exc(exc)
  831. # Abort recv() and pending pings with a ConnectionClosed exception.
  832. self.recv_messages.close()
  833. self.abort_pings()
  834. if self.keepalive_task is not None:
  835. self.keepalive_task.cancel()
  836. # If self.connection_lost_waiter isn't pending, that's a bug, because:
  837. # - it's set only here in connection_lost() which is called only once;
  838. # - it must never be canceled.
  839. self.connection_lost_waiter.set_result(None)
  840. # Adapted from asyncio.streams.FlowControlMixin
  841. if self.paused: # pragma: no cover
  842. self.paused = False
  843. for waiter in self.drain_waiters:
  844. if not waiter.done():
  845. if exc is None:
  846. waiter.set_result(None)
  847. else:
  848. waiter.set_exception(exc)
  849. # Flow control callbacks
  850. def pause_writing(self) -> None: # pragma: no cover
  851. # Adapted from asyncio.streams.FlowControlMixin
  852. assert not self.paused
  853. self.paused = True
  854. def resume_writing(self) -> None: # pragma: no cover
  855. # Adapted from asyncio.streams.FlowControlMixin
  856. assert self.paused
  857. self.paused = False
  858. for waiter in self.drain_waiters:
  859. if not waiter.done():
  860. waiter.set_result(None)
  861. async def drain(self) -> None: # pragma: no cover
  862. # We don't check if the connection is closed because we call drain()
  863. # immediately after write() and write() would fail in that case.
  864. # Adapted from asyncio.streams.StreamWriter
  865. # Yield to the event loop so that connection_lost() may be called.
  866. if self.transport.is_closing():
  867. await asyncio.sleep(0)
  868. # Adapted from asyncio.streams.FlowControlMixin
  869. if self.paused:
  870. waiter = self.loop.create_future()
  871. self.drain_waiters.append(waiter)
  872. try:
  873. await waiter
  874. finally:
  875. self.drain_waiters.remove(waiter)
  876. # Streaming protocol callbacks
  877. def data_received(self, data: bytes) -> None:
  878. # Feed incoming data to the protocol.
  879. self.protocol.receive_data(data)
  880. # This isn't expected to raise an exception.
  881. events = self.protocol.events_received()
  882. # Write outgoing data to the transport.
  883. try:
  884. self.send_data()
  885. except Exception as exc:
  886. if self.debug:
  887. self.logger.debug("! error while sending data", exc_info=True)
  888. self.set_recv_exc(exc)
  889. if self.protocol.close_expected():
  890. # If the connection is expected to close soon, set the
  891. # close deadline based on the close timeout.
  892. if self.close_timeout is not None:
  893. if self.close_deadline is None:
  894. self.close_deadline = self.loop.time() + self.close_timeout
  895. for event in events:
  896. # This isn't expected to raise an exception.
  897. self.process_event(event)
  898. def eof_received(self) -> None:
  899. # Feed the end of the data stream to the connection.
  900. self.protocol.receive_eof()
  901. # This isn't expected to raise an exception.
  902. events = self.protocol.events_received()
  903. # There is no error handling because send_data() can only write
  904. # the end of the data stream here and it shouldn't raise errors.
  905. self.send_data()
  906. # This code path is triggered when receiving an HTTP response
  907. # without a Content-Length header. This is the only case where
  908. # reading until EOF generates an event; all other events have
  909. # a known length. Ignore for coverage measurement because tests
  910. # are in test_client.py rather than test_connection.py.
  911. for event in events: # pragma: no cover
  912. # This isn't expected to raise an exception.
  913. self.process_event(event)
  914. # The WebSocket protocol has its own closing handshake: endpoints close
  915. # the TCP or TLS connection after sending and receiving a close frame.
  916. # As a consequence, they never need to write after receiving EOF, so
  917. # there's no reason to keep the transport open by returning True.
  918. # Besides, that doesn't work on TLS connections.
  919. # broadcast() is defined in the connection module even though it's primarily
  920. # used by servers and documented in the server module because it works with
  921. # client connections too and because it's easier to test together with the
  922. # Connection class.
  923. def broadcast(
  924. connections: Iterable[Connection],
  925. message: Data,
  926. raise_exceptions: bool = False,
  927. ) -> None:
  928. """
  929. Broadcast a message to several WebSocket connections.
  930. A string (:class:`str`) is sent as a Text_ frame. A bytestring or bytes-like
  931. object (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) is sent
  932. as a Binary_ frame.
  933. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  934. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  935. :func:`broadcast` pushes the message synchronously to all connections even
  936. if their write buffers are overflowing. There's no backpressure.
  937. If you broadcast messages faster than a connection can handle them, messages
  938. will pile up in its write buffer until the connection times out. Keep
  939. ``ping_interval`` and ``ping_timeout`` low to prevent excessive memory usage
  940. from slow connections.
  941. Unlike :meth:`~websockets.asyncio.connection.Connection.send`,
  942. :func:`broadcast` doesn't support sending fragmented messages. Indeed,
  943. fragmentation is useful for sending large messages without buffering them in
  944. memory, while :func:`broadcast` buffers one copy per connection as fast as
  945. possible.
  946. :func:`broadcast` skips connections that aren't open in order to avoid
  947. errors on connections where the closing handshake is in progress.
  948. :func:`broadcast` ignores failures to write the message on some connections.
  949. It continues writing to other connections. On Python 3.11 and above, you may
  950. set ``raise_exceptions`` to :obj:`True` to record failures and raise all
  951. exceptions in a :pep:`654` :exc:`ExceptionGroup`.
  952. While :func:`broadcast` makes more sense for servers, it works identically
  953. with clients, if you have a use case for opening connections to many servers
  954. and broadcasting a message to them.
  955. Args:
  956. websockets: WebSocket connections to which the message will be sent.
  957. message: Message to send.
  958. raise_exceptions: Whether to raise an exception in case of failures.
  959. Raises:
  960. TypeError: If ``message`` doesn't have a supported type.
  961. """
  962. if isinstance(message, str):
  963. send_method = "send_text"
  964. message = message.encode()
  965. elif isinstance(message, BytesLike):
  966. send_method = "send_binary"
  967. else:
  968. raise TypeError("data must be str or bytes")
  969. if raise_exceptions:
  970. if sys.version_info[:2] < (3, 11): # pragma: no cover
  971. raise ValueError("raise_exceptions requires at least Python 3.11")
  972. exceptions: list[Exception] = []
  973. for connection in connections:
  974. exception: Exception
  975. if connection.protocol.state is not OPEN:
  976. continue
  977. if connection.fragmented_send_waiter is not None:
  978. if raise_exceptions:
  979. exception = ConcurrencyError("sending a fragmented message")
  980. exceptions.append(exception)
  981. else:
  982. connection.logger.warning(
  983. "skipped broadcast: sending a fragmented message",
  984. )
  985. continue
  986. try:
  987. # Call connection.protocol.send_text or send_binary.
  988. # Either way, message is already converted to bytes.
  989. getattr(connection.protocol, send_method)(message)
  990. connection.send_data()
  991. except Exception as write_exception:
  992. if raise_exceptions:
  993. exception = RuntimeError("failed to write message")
  994. exception.__cause__ = write_exception
  995. exceptions.append(exception)
  996. else:
  997. connection.logger.warning(
  998. "skipped broadcast: failed to write message: %s",
  999. traceback.format_exception_only(
  1000. # Remove first argument when dropping Python 3.9.
  1001. type(write_exception),
  1002. write_exception,
  1003. )[0].strip(),
  1004. )
  1005. if raise_exceptions and exceptions:
  1006. raise ExceptionGroup("skipped broadcast", exceptions)
  1007. # Pretend that broadcast is actually defined in the server module.
  1008. broadcast.__module__ = "websockets.asyncio.server"