duck.contrib.websockets.frameยถ

WebSocket frame module.

Implements WebSocket frame parsing and serialization per RFC 6455.

Module Contentsยถ

Classesยถ

Frame

Represents a WebSocket frame according to RFC 6455.

Functionsยถ

apply_mask

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:

Frame

Raises:
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.