Data structures#
WebSocket events#
- class websockets.frames.Frame(opcode, data, fin=True, rsv1=False, rsv2=False, rsv3=False)#
WebSocket frame.
- opcode#
Opcode.
- Type:
Only these fields are needed. The MASK bit, payload length and masking-key are handled on the fly when parsing and serializing frames.
- class websockets.frames.Opcode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#
Opcode values for WebSocket frames.
- CONT = 0#
- TEXT = 1#
- BINARY = 2#
- CLOSE = 8#
- PING = 9#
- PONG = 10#
- class websockets.frames.Close(code, reason)#
Code and reason for WebSocket close frames.
- class websockets.frames.CloseCode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#
Close code values for WebSocket close frames.
- NORMAL_CLOSURE = 1000#
- GOING_AWAY = 1001#
- PROTOCOL_ERROR = 1002#
- UNSUPPORTED_DATA = 1003#
- NO_STATUS_RCVD = 1005#
- ABNORMAL_CLOSURE = 1006#
- INVALID_DATA = 1007#
- POLICY_VIOLATION = 1008#
- MESSAGE_TOO_BIG = 1009#
- MANDATORY_EXTENSION = 1010#
- INTERNAL_ERROR = 1011#
- SERVICE_RESTART = 1012#
- TRY_AGAIN_LATER = 1013#
- BAD_GATEWAY = 1014#
- TLS_HANDSHAKE = 1015#
HTTP events#
- class websockets.http11.Request(path, headers, _exception=None)#
WebSocket handshake request.
- headers#
Request headers.
- class websockets.http11.Response(status_code, reason_phrase, headers, body=None, _exception=None)#
WebSocket handshake response.
- headers#
Response headers.
- class websockets.datastructures.Headers(*args, **kwargs)#
Efficient data structure for manipulating HTTP headers.
A
listof(name, values)is inefficient for lookups.A
dictdoesn’t suffice because header names are case-insensitive and multiple occurrences of headers with the same name are possible.Headersstores HTTP headers in a hybrid data structure to provide efficient insertions and lookups while preserving the original data.In order to account for multiple values with minimal hassle,
Headersfollows this logic:- When getting a header with
headers[name]: if there’s no value,
KeyErroris raised;if there’s exactly one value, it’s returned;
if there’s more than one value,
MultipleValuesErroris raised.
- When getting a header with
When setting a header with
headers[name] = value, the value is appended to the list of values for that header.When deleting a header with
del headers[name], all values for that header are removed (this is slow).
Other methods for manipulating headers are consistent with this logic.
As long as no header occurs multiple times,
Headersbehaves likedict, except keys are lower-cased to provide case-insensitivity.Two methods support manipulating multiple values explicitly:
get_all()returns a list of all values for a header;raw_items()returns an iterator of(name, values)pairs.
- get_all(key)#
Return the (possibly empty) list of all values for a header.
- Parameters:
key (str) – header name.
- raw_items()#
Return an iterator of all values as
(name, value)pairs.
URIs#
- websockets.uri.parse_uri(uri)#
Parse and validate a WebSocket URI.
- Parameters:
uri (str) – WebSocket URI.
- Returns:
Parsed WebSocket URI.
- Return type:
- Raises:
InvalidURI – if
uriisn’t a valid WebSocket URI.
- class websockets.uri.WebSocketURI(secure, host, port, path, query, username=None, password=None)#
WebSocket URI.
- username#
Available when the URI contains User Information.
- password#
Available when the URI contains User Information.