autobahn.websocket

Submodules

Attributes

HAS_NVX

Boolean flag indicating whether NVX (Native Vector Extensions) native acceleration

USES_NVX

Exceptions

ConnectionDeny

Throw an instance of this class to deny a WebSocket connection

Classes

ConnectionAccept

Used by WebSocket servers to accept an incoming WebSocket connection.

ConnectionRequest

Thin-wrapper for WebSocket connection request information provided in

ConnectionResponse

Thin-wrapper for WebSocket connection response information provided in

IWebSocketChannel

A WebSocket channel is a bidirectional, full-duplex, ordered, reliable message channel

IncomingMessage

An incoming WebSocket message.

Message

Abstract base class for WebSocket messages.

OutgoingMessage

An outgoing WebSocket message.

Package Contents

class ConnectionAccept(subprotocol=None, headers=None)[source]

Bases: object

Used by WebSocket servers to accept an incoming WebSocket connection. If the client announced one or multiple subprotocols, the server MUST select one of the subprotocols announced by the client.

__slots__ = ('subprotocol', 'headers')
headers = None
subprotocol = None
exception ConnectionDeny(code, reason=None)[source]

Bases: Exception

Throw an instance of this class to deny a WebSocket connection during handshake in autobahn.websocket.protocol.WebSocketServerProtocol.onConnect().

BAD_REQUEST = 400

Bad Request. The request cannot be fulfilled due to bad syntax.

FORBIDDEN = 403

Forbidden. The request was a legal request, but the server is refusing to respond to it.[2] Unlike a 401 Unauthorized response, authenticating will make no difference.

INTERNAL_SERVER_ERROR = 500

Internal Server Error. A generic error message, given when no more specific message is suitable.

NOT_ACCEPTABLE = 406

Not Acceptable. The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

NOT_FOUND = 404

Not Found. The requested resource could not be found but may be available again in the future.[2] Subsequent requests by the client are permissible.

NOT_IMPLEMENTED = 501

Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfill the request.

REQUEST_TIMEOUT = 408

‘The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time.

Type:

Request Timeout. The server timed out waiting for the request. According to W3 HTTP specifications

SERVICE_UNAVAILABLE = 503

Service Unavailable. The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.

__slots__ = ('code', 'reason')
code
reason = None
class ConnectionRequest(peer, headers, host, path, params, version, origin, protocols, extensions)[source]

Bases: object

Thin-wrapper for WebSocket connection request information provided in autobahn.websocket.protocol.WebSocketServerProtocol.onConnect() when a WebSocket client want to establish a connection to a WebSocket server.

__json__()[source]
__slots__ = ('peer', 'headers', 'host', 'path', 'params', 'version', 'origin', 'protocols', 'extensions')
__str__()[source]
extensions
headers
host
origin
params
path
peer
protocols
version
class ConnectionResponse(peer, headers, version, protocol, extensions)[source]

Bases: object

Thin-wrapper for WebSocket connection response information provided in autobahn.websocket.protocol.WebSocketClientProtocol.onConnect() when a WebSocket server has accepted a connection request by a client.

__json__()[source]
__slots__ = ('peer', 'headers', 'version', 'protocol', 'extensions')
__str__()[source]
extensions
headers
peer
protocol
version
HAS_NVX = False[source]

Boolean flag indicating whether NVX (Native Vector Extensions) native acceleration modules are available in this installation.

This is a build-time capability check - it’s True if the NVX native extensions were successfully compiled and can be imported, False otherwise.

NVX provides native implementations for performance-critical WebSocket operations:

  • UTF-8 validation using SIMD instructions

  • XOR masking using vectorized operations

The value of HAS_NVX is independent of the runtime setting AUTOBAHN_USE_NVX. To check if NVX is actually being used at runtime, see USES_NVX.

Type:

bool

Example:

from autobahn.websocket import HAS_NVX

if HAS_NVX:
    print("NVX native acceleration is available")
else:
    print("NVX not built - using pure Python implementations")

See also

USES_NVX - Whether NVX is actually enabled at runtime

class IWebSocketChannel[source]

Bases: abc.ABC

A WebSocket channel is a bidirectional, full-duplex, ordered, reliable message channel over a WebSocket connection as specified in RFC6455.

This interface defines a message-based API to WebSocket plus auxiliary hooks and methods.

When a WebSocket connection is established, the following callbacks are fired:

Once a WebSocket connection is open, messages can be sent and received using:

The WebSocket connection can be closed and closing observed using:

Finally, WebSocket ping/pong messages

which are used for e.g. for heart-beating.

abstractmethod onClose(wasClean: bool, code: int, reason: str)[source]

Callback fired when the WebSocket connection has been closed (WebSocket closing handshake has been finished or the connection was closed uncleanly).

Parameters:
  • wasCleanTrue iff the WebSocket connection was closed cleanly.

  • code – Close status code as sent by the WebSocket peer.

  • reason – Close reason as sent by the WebSocket peer.

abstractmethod onConnect(request_or_response: autobahn.websocket.types.ConnectionRequest | autobahn.websocket.types.ConnectionResponse) str | None | Tuple[str | None, Dict[str, str]][source]

Callback fired during WebSocket opening handshake when a client connects to a server with request with a ConnectionRequest from the client or when a server connection was established by a client with a ConnectionResponse response from the server.

This method may run asynchronously.

Parameters:

request_or_response (Instance of autobahn.websocket.types.ConnectionRequest or autobahn.websocket.types.ConnectionResponse.) – Connection request (for servers) or response (for clients).

Returns:

When this callback is fired on a WebSocket server, you may return one of: None: the connection is accepted with no specific WebSocket subprotocol, str: the connection is accepted with the returned name as the WebSocket subprotocol, or (str, dict): a pair of subprotocol accepted and HTTP headers to send to the client. When this callback is fired on a WebSocket client, this method must return None. To deny a connection (client and server), raise an Exception. You can also return a Deferred/Future that resolves/rejects to the above.

abstractmethod onConnecting(transport_details: autobahn.wamp.types.TransportDetails) autobahn.websocket.types.ConnectingRequest | None[source]

This method is called when the WebSocket peer is connected at the byte stream level (e.g. TCP, TLS or Serial), but before the WebSocket opening handshake (e.g. at the HTTP request level).

Parameters:

transport_details – information about the transport.

Returns:

A autobahn.websocket.types.ConnectingRequest instance is returned to indicate which options should be used for this connection. If you wish to use the default behavior, None may be returned (this is the default).

abstractmethod onMessage(payload: bytes, isBinary: bool)[source]

Callback fired when a complete WebSocket message was received.

This method may run asynchronously.

Parameters:
  • payload – The WebSocket message received.

  • isBinary – Flag indicating whether payload is binary or UTF-8 encoded text.

abstractmethod onOpen()[source]

Callback fired when the initial WebSocket opening handshake was completed. You now can send and receive WebSocket messages.

This method may run asynchronously.

abstractmethod onPing(payload: bytes)[source]

Callback fired when a WebSocket ping was received. A default implementation responds by sending a WebSocket pong.

Parameters:

payload – Payload of ping (when there was any). Can be arbitrary, up to 125 octets.

abstractmethod onPong(payload: bytes)[source]

Callback fired when a WebSocket pong was received. A default implementation does nothing.

Parameters:

payload – Payload of pong (when there was any). Can be arbitrary, up to 125 octets.

abstractmethod sendClose(code: int | None = None, reason: str | None = None)[source]

Starts a WebSocket closing handshake tearing down the WebSocket connection.

Parameters:
  • code – An optional close status code (1000 for normal close or 3000-4999 for application specific close).

  • reason – An optional close reason (a string that when present, a status code MUST also be present).

abstractmethod sendMessage(payload: bytes, isBinary: bool)[source]

Send a WebSocket message over the connection to the peer.

Parameters:
  • payload – The WebSocket message to be sent.

  • isBinary – Flag indicating whether payload is binary or UTF-8 encoded text.

abstractmethod sendPing(payload: bytes | None = None)[source]

Send a WebSocket ping to the peer.

A peer is expected to pong back the payload a soon as “practical”. When more than one ping is outstanding at a peer, the peer may elect to respond only to the last ping.

Parameters:

payload – An (optional) arbitrary payload of length less than 126 octets.

abstractmethod sendPong(payload: bytes | None = None)[source]

Send a WebSocket pong to the peer.

A WebSocket pong may be sent unsolicited. This serves as a unidirectional heartbeat. A response to an unsolicited pong is “not expected”.

Parameters:

payload – An (optional) arbitrary payload of length < 126 octets.

class IncomingMessage(payload, is_binary=False)[source]

Bases: Message

An incoming WebSocket message.

__slots__ = ('payload', 'is_binary')
is_binary = False
payload
class Message[source]

Bases: object

Abstract base class for WebSocket messages.

__slots__ = ()
class OutgoingMessage(payload, is_binary=False, skip_compress=False)[source]

Bases: Message

An outgoing WebSocket message.

__slots__ = ('payload', 'is_binary', 'skip_compress')
is_binary = False
payload
skip_compress = False
USES_NVX = False[source]