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.