WebSocket utilities

class aiohttp.WSCloseCode[source]

An IntEnum for keeping close message code.

OK

A normal closure, meaning that the purpose for which the connection was established has been fulfilled.

GOING_AWAY

An endpoint is “going away”, such as a server going down or a browser having navigated away from a page.

PROTOCOL_ERROR

An endpoint is terminating the connection due to a protocol error.

UNSUPPORTED_DATA

An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).

INVALID_TEXT

An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 RFC 3629 data within a text message).

POLICY_VIOLATION

An endpoint is terminating the connection because it has received a message that violates its policy. This is a generic status code that can be returned when there is no other more suitable status code (e.g., UNSUPPORTED_DATA or MESSAGE_TOO_BIG) or if there is a need to hide specific details about the policy.

MESSAGE_TOO_BIG

An endpoint is terminating the connection because it has received a message that is too big for it to process.

MANDATORY_EXTENSION

An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server did not return them in the response message of the WebSocket handshake. The list of extensions that are needed should appear in the /reason/ part of the Close frame. Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.

INTERNAL_ERROR

A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.

SERVICE_RESTART

The service is restarted. a client may reconnect, and if it chooses to do, should reconnect using a randomized delay of 5-30s.

TRY_AGAIN_LATER

The service is experiencing overload. A client should only connect to a different IP (when there are multiple for the target) or reconnect to the same IP upon user action.

ABNORMAL_CLOSURE

Used to indicate that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected.

BAD_GATEWAY

The server was acting as a gateway or proxy and received an invalid response from the upstream server. This is similar to 502 HTTP Status Code.

class aiohttp.WSMsgType[source]

An IntEnum for describing WSMessage type.

CONTINUATION

A mark for continuation frame, user will never get the message with this type.

TEXT

Text message, the value has str type.

BINARY

Binary message, the value has bytes type.

PING

Ping frame (sent by client peer).

PONG

Pong frame, answer on ping. Sent by server peer.

CLOSE

Close frame.

CLOSED FRAME

Actually not frame but a flag indicating that websocket was closed.

ERROR

Actually not frame but a flag indicating that websocket was received an error.

class aiohttp.WSMessage

Websocket message, returned by .receive() calls. This is actually defined as a typing.Union of different message types. All messages are a collections.namedtuple() with the below attributes.

data

Message payload.

  1. str for WSMsgType.TEXT messages.

  2. bytes for WSMsgType.BINARY messages.

  3. int (see WSCloseCode for common codes) for WSMsgType.CLOSE messages.

  4. bytes for WSMsgType.PING messages.

  5. bytes for WSMsgType.PONG messages.

  6. Exception for WSMsgType.ERROR messages.

extra

Additional info, str if provided, otherwise defaults to None.

Makes sense only for WSMsgType.CLOSE messages, contains optional message description.

type

Message type, WSMsgType instance.

json(*, loads=json.loads)

Returns parsed JSON data (the method is only present on WSMsgType.TEXT and WSMsgType.BINARY messages).

Parameters:

loads – optional JSON decoder function.