connection.py 48 KB

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