Exceptions#

websockets.exceptions defines the following exception hierarchy:

exception websockets.exceptions.AbortHandshake(status, headers, body=b'')[source]#

Raised to abort the handshake on purpose and return an HTTP response.

This exception is an implementation detail.

The public API is process_request().

status#

HTTP status code.

Type:

HTTPStatus

headers#

HTTP response headers.

Type:

Headers

body#

HTTP response body.

Type:

bytes

exception websockets.exceptions.ConnectionClosed(rcvd, sent, rcvd_then_sent=None)[source]#

Raised when trying to interact with a closed connection.

rcvd#

if a close frame was received, its code and reason are available in rcvd.code and rcvd.reason.

Type:

Optional[Close]

sent#

if a close frame was sent, its code and reason are available in sent.code and sent.reason.

Type:

Optional[Close]

rcvd_then_sent#

if close frames were received and sent, this attribute tells in which order this happened, from the perspective of this side of the connection.

Type:

Optional[bool]

exception websockets.exceptions.ConnectionClosedError(rcvd, sent, rcvd_then_sent=None)[source]#

Like ConnectionClosed, when the connection terminated with an error.

A close frame with a code other than 1000 (OK) or 1001 (going away) was received or sent, or the closing handshake didn’t complete properly.

exception websockets.exceptions.ConnectionClosedOK(rcvd, sent, rcvd_then_sent=None)[source]#

Like ConnectionClosed, when the connection terminated properly.

A close code with code 1000 (OK) or 1001 (going away) or without a code was received and sent.

exception websockets.exceptions.DuplicateParameter(name)[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, value=None)[source]#

Raised when an HTTP header doesn’t have a valid format or value.

exception websockets.exceptions.InvalidHeaderFormat(name, error, header, pos)[source]#

Raised when an HTTP header cannot be parsed.

The format of the header doesn’t match the grammar for that header.

exception websockets.exceptions.InvalidHeaderValue(name, value=None)[source]#

Raised when an 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)[source]#

Raised when the Origin header in a request isn’t allowed.

exception websockets.exceptions.InvalidParameterName(name)[source]#

Raised when a parameter name in an extension header is invalid.

exception websockets.exceptions.InvalidParameterValue(name, value)[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.InvalidStatus(response)[source]#

Raised when a handshake response rejects the WebSocket upgrade.

exception websockets.exceptions.InvalidStatusCode(status_code, headers)[source]#

Raised when a handshake response status code is invalid.

exception websockets.exceptions.InvalidURI(uri, msg)[source]#

Raised when connecting to a URI that isn’t a valid WebSocket URI.

exception websockets.exceptions.InvalidUpgrade(name, value=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.ProtocolError[source]#

Raised when a frame breaks the protocol.

exception websockets.exceptions.RedirectHandshake(uri)[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[source]#

alias of ProtocolError