autobahn.twisted.websocket


Module Contents

Classes

WebSocketAdapterProtocol

Adapter class for Twisted WebSocket client and server protocols.

WebSocketServerProtocol

Base class for Twisted-based WebSocket server protocols.

WebSocketClientProtocol

Base class for Twisted-based WebSocket client protocols.

WebSocketAdapterFactory

Adapter class for Twisted-based WebSocket client and server factories.

WebSocketServerFactory

Base class for Twisted-based WebSocket server factories.

WebSocketClientFactory

Base class for Twisted-based WebSocket client factories.

WrappingWebSocketAdapter

An adapter for stream-based transport over WebSocket.

WrappingWebSocketServerProtocol

Server protocol for stream-based transport over WebSocket.

WrappingWebSocketClientProtocol

Client protocol for stream-based transport over WebSocket.

WrappingWebSocketServerFactory

Wrapping server factory for stream-based transport over WebSocket.

WrappingWebSocketClientFactory

Wrapping client factory for stream-based transport over WebSocket.

WampWebSocketServerProtocol

Twisted-based WAMP-over-WebSocket server protocol.

WampWebSocketServerFactory

Twisted-based WAMP-over-WebSocket server protocol factory.

WampWebSocketClientProtocol

Twisted-based WAMP-over-WebSocket client protocol.

WampWebSocketClientFactory

Twisted-based WAMP-over-WebSocket client protocol factory.

Functions

create_client_agent(reactor)

returns

an instance implementing IWebSocketClientAgent

connectWS(factory[, contextFactory, timeout, bindAddress])

Establish WebSocket connection to a server. The connection parameters like target

listenWS(factory[, contextFactory, backlog, interface])

Listen for incoming WebSocket connections from clients. The connection parameters like

autobahn.twisted.websocket.create_client_agent(reactor)[source]
Returns

an instance implementing IWebSocketClientAgent

class autobahn.twisted.websocket.WebSocketAdapterProtocol[source]

Bases: twisted.internet.protocol.Protocol

Adapter class for Twisted WebSocket client and server protocols.

Called from Twisted:

Called from Network-independent Code (WebSocket implementation):

FIXME:

log
peer :Optional[str]
is_server :Optional[bool]
connectionMade()[source]

Called when a connection is made.

This may be considered the initializer of the protocol, because it is called when the connection is completed. For clients, this is called once the connection to the server has been established; for servers, this is called after an accept() call stops blocking and a socket has been received. If you need to send any greeting or initial message, do it here.

connectionLost(reason: twisted.python.failure.Failure = connectionDone)[source]

Called when the connection is shut down.

Clear any circular references here, and any external references to this Protocol. The connection has been closed.

@type reason: L{twisted.python.failure.Failure}

dataReceived(data: bytes)[source]

Called whenever data is received.

Use this method to translate to a higher-level message. Usually, some callback will be made upon the receipt of each complete protocol message.

@param data: a string of indeterminate length. Please keep in mind

that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received! I recommend that unit tests for protocols call through to this method with differing chunk sizes, down to one byte at a time.

_closeConnection(abort=False)[source]
_onOpen()[source]
_onMessageBegin(isBinary)[source]
_onMessageFrameBegin(length)[source]
_onMessageFrameData(payload)[source]
_onMessageFrameEnd()[source]
_onMessageFrame(payload)[source]
_onMessageEnd()[source]
_onMessage(payload, isBinary)[source]
_onPing(payload)[source]
_onPong(payload)[source]
_onClose(wasClean, code, reason)[source]
registerProducer(producer, streaming)[source]

Register a Twisted producer with this protocol.

Parameters
  • producer (object) – A Twisted push or pull producer.

  • streaming (bool) – Producer type.

unregisterProducer()[source]

Unregister Twisted producer with this protocol.

class autobahn.twisted.websocket.WebSocketServerProtocol[source]

Bases: WebSocketAdapterProtocol, autobahn.websocket.protocol.WebSocketServerProtocol

Base class for Twisted-based WebSocket server protocols.

Implements autobahn.websocket.interfaces.IWebSocketChannel.

log
is_server = True
class autobahn.twisted.websocket.WebSocketClientProtocol[source]

Bases: WebSocketAdapterProtocol, autobahn.websocket.protocol.WebSocketClientProtocol

Base class for Twisted-based WebSocket client protocols.

Implements autobahn.websocket.interfaces.IWebSocketChannel.

log
is_server = False
_onConnect(response: autobahn.websocket.types.ConnectionResponse)[source]
startTLS()[source]
class autobahn.twisted.websocket.WebSocketAdapterFactory[source]

Bases: object

Adapter class for Twisted-based WebSocket client and server factories.

class autobahn.twisted.websocket.WebSocketServerFactory(*args, **kwargs)[source]

Bases: WebSocketAdapterFactory, autobahn.websocket.protocol.WebSocketServerFactory, twisted.internet.protocol.ServerFactory

Base class for Twisted-based WebSocket server factories.

Implements autobahn.websocket.interfaces.IWebSocketServerChannelFactory

log
class autobahn.twisted.websocket.WebSocketClientFactory(*args, **kwargs)[source]

Bases: WebSocketAdapterFactory, autobahn.websocket.protocol.WebSocketClientFactory, twisted.internet.protocol.ClientFactory

Base class for Twisted-based WebSocket client factories.

Implements autobahn.websocket.interfaces.IWebSocketClientChannelFactory

log
class autobahn.twisted.websocket.WrappingWebSocketAdapter[source]

Bases: object

An adapter for stream-based transport over WebSocket.

This follows websockify and should be compatible with that.

It uses WebSocket subprotocol negotiation and supports the following WebSocket subprotocols:

  • binary (or a compatible subprotocol)

  • base64

Octets are either transmitted as the payload of WebSocket binary messages when using the binary subprotocol (or an alternative binary compatible subprotocol), or encoded with Base64 and then transmitted as the payload of WebSocket text messages when using the base64 subprotocol.

onConnect(requestOrResponse)[source]
onOpen()[source]
onMessage(payload, isBinary)[source]
onClose(wasClean, code, reason)[source]
write(data)[source]
writeSequence(data)[source]
loseConnection()[source]
getPeer()[source]
getHost()[source]
class autobahn.twisted.websocket.WrappingWebSocketServerProtocol[source]

Bases: WrappingWebSocketAdapter, WebSocketServerProtocol

Server protocol for stream-based transport over WebSocket.

class autobahn.twisted.websocket.WrappingWebSocketClientProtocol[source]

Bases: WrappingWebSocketAdapter, WebSocketClientProtocol

Client protocol for stream-based transport over WebSocket.

class autobahn.twisted.websocket.WrappingWebSocketServerFactory(factory, url, reactor=None, enableCompression=True, autoFragmentSize=0, subprotocol=None)[source]

Bases: WebSocketServerFactory

Wrapping server factory for stream-based transport over WebSocket.

buildProtocol(addr)[source]

Create an instance of a subclass of Protocol.

The returned instance will handle input on an incoming server connection, and an attribute “factory” pointing to the creating factory.

Alternatively, L{None} may be returned to immediately close the new connection.

Override this method to alter how Protocol instances get created.

@param addr: an object implementing L{IAddress}

startFactory()[source]

This will be called before I begin listening on a Port or Connector.

It will only be called once, even if the factory is connected to multiple ports.

This can be used to perform ‘unserialization’ tasks that are best put off until things are actually running, such as connecting to a database, opening files, etcetera.

stopFactory()[source]

This will be called before I stop listening on all Ports/Connectors.

This can be overridden to perform ‘shutdown’ tasks such as disconnecting database connections, closing files, etc.

It will be called, for example, before an application shuts down, if it was connected to a port. User code should not call this function directly.

class autobahn.twisted.websocket.WrappingWebSocketClientFactory(factory, url, reactor=None, enableCompression=True, autoFragmentSize=0, subprotocol=None)[source]

Bases: WebSocketClientFactory

Wrapping client factory for stream-based transport over WebSocket.

buildProtocol(addr)[source]

Create an instance of a subclass of Protocol.

The returned instance will handle input on an incoming server connection, and an attribute “factory” pointing to the creating factory.

Alternatively, L{None} may be returned to immediately close the new connection.

Override this method to alter how Protocol instances get created.

@param addr: an object implementing L{IAddress}

autobahn.twisted.websocket.connectWS(factory, contextFactory=None, timeout=30, bindAddress=None)[source]

Establish WebSocket connection to a server. The connection parameters like target host, port, resource and others are provided via the factory.

Parameters
  • factory (An autobahn.websocket.WebSocketClientFactory instance.) – The WebSocket protocol factory to be used for creating client protocol instances.

  • contextFactory (A twisted.internet.ssl.ClientContextFactory instance.) – SSL context factory, required for secure WebSocket connections (“wss”).

  • timeout (int) – Number of seconds to wait before assuming the connection has failed.

  • bindAddress (tuple) – A (host, port) tuple of local address to bind to, or None.

Returns

The connector.

Return type

An object which implements twisted.interface.IConnector.

autobahn.twisted.websocket.listenWS(factory, contextFactory=None, backlog=50, interface='')[source]

Listen for incoming WebSocket connections from clients. The connection parameters like listening port and others are provided via the factory.

Parameters
  • factory (An autobahn.websocket.WebSocketServerFactory instance.) – The WebSocket protocol factory to be used for creating server protocol instances.

  • contextFactory (A twisted.internet.ssl.ContextFactory.) – SSL context factory, required for secure WebSocket connections (“wss”).

  • backlog (int) – Size of the listen queue.

  • interface (str) – The interface (derived from hostname given) to bind to, defaults to ‘’ (all).

Returns

The listening port.

Return type

An object that implements twisted.interface.IListeningPort.

class autobahn.twisted.websocket.WampWebSocketServerProtocol[source]

Bases: autobahn.wamp.websocket.WampWebSocketServerProtocol, WebSocketServerProtocol

Twisted-based WAMP-over-WebSocket server protocol.

Implements:

class autobahn.twisted.websocket.WampWebSocketServerFactory(factory, *args, **kwargs)[source]

Bases: autobahn.wamp.websocket.WampWebSocketServerFactory, WebSocketServerFactory

Twisted-based WAMP-over-WebSocket server protocol factory.

protocol
class autobahn.twisted.websocket.WampWebSocketClientProtocol[source]

Bases: autobahn.wamp.websocket.WampWebSocketClientProtocol, WebSocketClientProtocol

Twisted-based WAMP-over-WebSocket client protocol.

Implements:

class autobahn.twisted.websocket.WampWebSocketClientFactory(factory, *args, **kwargs)[source]

Bases: autobahn.wamp.websocket.WampWebSocketClientFactory, WebSocketClientFactory

Twisted-based WAMP-over-WebSocket client protocol factory.

protocol