MCPcopy Create free account
hub / github.com/python-websockets/websockets / read_exact

Method read_exact

src/websockets/streams.py:54–75  ·  view source on GitHub ↗

Read a given number of bytes from the stream. This is a generator-based coroutine. Args: n: How many bytes to read. Raises: EOFError: If the stream ends in less than ``n`` bytes.

(self, n: int)

Source from the content-addressed store, hash-verified

52 return r
53
54 def read_exact(self, n: int) -> Generator[None, None, bytearray]:
55 """
56 Read a given number of bytes from the stream.
57
58 This is a generator-based coroutine.
59
60 Args:
61 n: How many bytes to read.
62
63 Raises:
64 EOFError: If the stream ends in less than ``n`` bytes.
65
66 """
67 assert n >= 0
68 while len(self.buffer) < n:
69 if self.eof:
70 p = len(self.buffer)
71 raise EOFError(f"stream ends after {p} bytes, expected {n} bytes")
72 yield
73 r = self.buffer[:n]
74 del self.buffer[:n]
75 return r
76
77 def read_to_eof(self, m: int) -> Generator[None, None, bytearray]:
78 """

Calls

no outgoing calls