duck.contrib.websockets.frame¶
WebSocket frame module.
Implements WebSocket frame parsing and serialization per RFC 6455.
Module Contents¶
Classes¶
Represents a WebSocket frame according to RFC 6455. |
Functions¶
Applies a 4-byte WebSocket mask to the given data. |
API¶
- class duck.contrib.websockets.frame.Frame(opcode: int, fin: Optional[bool] = True, payload: bytes = b'', rsv1: bool = False, rsv2: bool = False, rsv3: bool = False)¶
Represents a WebSocket frame according to RFC 6455.
Initialization
- __repr__()¶
- __slots__¶
(‘opcode’, ‘fin’, ‘payload’, ‘rsv1’, ‘rsv2’, ‘rsv3’)
- __str__¶
None
- check() None¶
Check that reserved bits and opcode have acceptable values.
- Raises:
ProtocolError – If RSV bits are non-zero or control frame rules are violated.
- async classmethod parse(read_exact: Callable[[int], bytes], mask_required: bool = True, max_size: Optional[int] = None, extensions: Optional[Sequence[duck.contrib.websockets.extensions.Extension]] = None) duck.contrib.websockets.frame.Frame¶
Parses a WebSocket frame from the connection.
- Parameters:
read_exact – Coroutine to read an exact number of bytes.
mask_required – Whether to expect and validate masking (clients only).
max_size – Optional maximum size of frame payload.
extensions – Extensions to apply for decoding.
- Returns:
Parsed and decoded WebSocket frame.
- Return type:
- Raises:
ProtocolError – If the frame format is invalid.
PayloadTooBig – If the payload exceeds max_size.
- serialize(mask: bool = False, extensions: Optional[Sequence[duck.contrib.websockets.extensions.Extension]] = None) bytes¶
Serializes the frame into raw bytes ready to send.
- Parameters:
mask – Whether to apply masking (typically True for clients).
extensions – Extensions to apply on the fly.
- Returns:
The serialized WebSocket frame.
- Return type:
bytes
- duck.contrib.websockets.frame.apply_mask(data: bytes, mask: bytes) bytes¶
Applies a 4-byte WebSocket mask to the given data.