Utilities¶
Data structures¶
websockets.datastructures
defines a class for manipulating HTTP headers.
-
class
websockets.datastructures.
Headers
(*args: Any, **kwargs: str)[source]¶ Efficient data structure for manipulating HTTP headers.
A
list
of(name, values)
is inefficient for lookups.A
dict
doesn’t suffice because header names are case-insensitive and multiple occurrences of headers with the same name are possible.Headers
stores 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,
Headers
follows this logic:- When getting a header with
headers[name]
: if there’s no value,
KeyError
is raised;if there’s exactly one value, it’s returned;
if there’s more than one value,
MultipleValuesError
is 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,
Headers
behaves 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.
Exceptions¶
websockets.exceptions
defines the following exception hierarchy:
-
exception
websockets.exceptions.
AbortHandshake
(status: http.HTTPStatus, headers: Union[websockets.datastructures.Headers, Mapping[str, str], Iterable[Tuple[str, str]]], body: bytes = b'')[source]¶ Raised to abort the handshake on purpose and return a HTTP response.
This exception is an implementation detail.
The public API is
process_request()
.
-
exception
websockets.exceptions.
ConnectionClosed
(code: int, reason: str)[source]¶ Raised when trying to interact with a closed connection.
Provides the connection close code and reason in its
code
andreason
attributes respectively.
-
exception
websockets.exceptions.
ConnectionClosedError
(code: int, reason: str)[source]¶ Like
ConnectionClosed
, when the connection terminated with an error.This means the close code is different from 1000 (OK) and 1001 (going away).
-
exception
websockets.exceptions.
ConnectionClosedOK
(code: int, reason: str)[source]¶ Like
ConnectionClosed
, when the connection terminated properly.This means the close code is 1000 (OK) or 1001 (going away).
-
exception
websockets.exceptions.
DuplicateParameter
(name: str)[source]¶ Raised when a parameter name is repeated in an extension header.
-
exception
websockets.exceptions.
InvalidHandshake
[source]¶ Raised during the handshake when the WebSocket connection fails.
-
exception
websockets.exceptions.
InvalidHeader
(name: str, value: Optional[str] = None)[source]¶ Raised when a HTTP header doesn’t have a valid format or value.
-
exception
websockets.exceptions.
InvalidHeaderFormat
(name: str, error: str, header: str, pos: int)[source]¶ Raised when a HTTP header cannot be parsed.
The format of the header doesn’t match the grammar for that header.
-
exception
websockets.exceptions.
InvalidHeaderValue
(name: str, value: Optional[str] = None)[source]¶ Raised when a HTTP header has a wrong value.
The format of the header is correct but a value isn’t acceptable.
-
exception
websockets.exceptions.
InvalidMessage
[source]¶ Raised when a handshake request or response is malformed.
-
exception
websockets.exceptions.
InvalidOrigin
(origin: Optional[str])[source]¶ Raised when the Origin header in a request isn’t allowed.
-
exception
websockets.exceptions.
InvalidParameterName
(name: str)[source]¶ Raised when a parameter name in an extension header is invalid.
-
exception
websockets.exceptions.
InvalidParameterValue
(name: str, value: Optional[str])[source]¶ Raised when a parameter value in an extension header is invalid.
-
exception
websockets.exceptions.
InvalidState
[source]¶ Raised when an operation is forbidden in the current state.
This exception is an implementation detail.
It should never be raised in normal circumstances.
-
exception
websockets.exceptions.
InvalidStatusCode
(status_code: int)[source]¶ Raised when a handshake response status code is invalid.
The integer status code is available in the
status_code
attribute.
-
exception
websockets.exceptions.
InvalidURI
(uri: str)[source]¶ Raised when connecting to an URI that isn’t a valid WebSocket URI.
-
exception
websockets.exceptions.
InvalidUpgrade
(name: str, value: Optional[str] = None)[source]¶ Raised when the Upgrade or Connection header isn’t correct.
-
exception
websockets.exceptions.
NegotiationError
[source]¶ Raised when negotiating an extension fails.
-
exception
websockets.exceptions.
PayloadTooBig
[source]¶ Raised when receiving a frame with a payload exceeding the maximum size.
-
exception
websockets.exceptions.
RedirectHandshake
(uri: str)[source]¶ Raised when a handshake gets redirected.
This exception is an implementation detail.
-
exception
websockets.exceptions.
SecurityError
[source]¶ Raised when a handshake request or response breaks a security rule.
Security limits are hard coded.
-
exception
websockets.exceptions.
WebSocketException
[source]¶ Base class for all exceptions defined by
websockets
.
-
websockets.exceptions.
WebSocketProtocolError
¶ alias of
websockets.exceptions.ProtocolError