Module autobahn.asyncio

Autobahn asyncio specific classes. These are used when asyncio is run as the underlying networking framework.

Component

The component API provides a high-level funcional style method of defining and running WAMP components including authentication configuration

class autobahn.asyncio.component.Component(main=None, transports=None, config=None, realm=u'realm1', extra=None, authentication=None, session_factory=None, is_fatal=None)[source]

A component establishes a transport and attached a session to a realm using the transport for communication.

The transports a component tries to use can be configured, as well as the auto-reconnect strategy.

Parameters:
  • main (callable taking 2 args: reactor, ISession) – After a transport has been connected and a session has been established and joined to a realm, this (async) procedure will be run until it finishes – which signals that the component has run to completion. In this case, it usually doesn’t make sense to use the on_* kwargs. If you do not pass a main() procedure, the session will not be closed (unless you arrange for .leave() to be called).
  • transports (None or unicode or list of dicts) –

    Transport configurations for creating transports. Each transport can be a WAMP URL, or a dict containing the following configuration keys:

    • type (optional): websocket (default) or rawsocket
    • url: the router URL
    • endpoint (optional, derived from URL if not provided):
      • type: “tcp” or “unix”
      • host, port: only for TCP
      • path: only for unix
      • timeout: in seconds
      • tls: True or (under Twisted) an twisted.internet.ssl.IOpenSSLClientComponentCreator instance (such as returned from twisted.internet.ssl.optionsForClientTLS) or CertificateOptions instance.
  • realm (unicode) – the realm to join
  • authentication (dict mapping auth_type to dict) – configuration of authenticators
  • session_factory (callable) – if None, ApplicationSession is used, otherwise a callable taking a single config argument that is used to create a new ApplicationSession instance.
  • is_fatal (callable taking one arg, or None) – a callable taking a single argument, an Exception instance. The callable should return True if this error is “fatal”, meaning we should not try connecting to the current transport again. The default behavior (on None) is to always return False
session_factory

alias of autobahn.asyncio.wamp.Session

start(loop=None)[source]

This starts the Component, which means it will start connecting (and re-connecting) to its configured transports. A Component runs until it is “done”, which means one of: - There was a “main” function defined, and it completed successfully; - Something called .leave() on our session, and we left successfully; - .stop() was called, and completed successfully; - none of our transports were able to connect successfully (failure);

Returns:a Future which will resolve (to None) when we are “done” or with an error if something went wrong.
autobahn.asyncio.component.run(components, start_loop=True, log_level='info')[source]

High-level API to run a series of components.

This will only return once all the components have stopped (including, possibly, after all re-connections have failed if you have re-connections enabled). Under the hood, this calls

XXX fixme for asyncio

– if you wish to manage the loop yourself, use the autobahn.asyncio.component.Component.start() method to start each component yourself.

Parameters:
  • components (instance or list of autobahn.asyncio.component.Component) – the Component(s) you wish to run
  • start_loop (bool) – When True (the default) this method start a new asyncio loop.
  • log_level (string) – a valid log-level (or None to avoid calling start_logging)

WebSocket Protocols and Factories

Classes for WebSocket clients and servers using asyncio.

class autobahn.asyncio.websocket.WebSocketServerProtocol[source]

Base class for asyncio-based WebSocket server protocols.

Implements:

get_channel_id(channel_id_type=u'tls-unique')[source]

Implements autobahn.wamp.interfaces.ITransport.get_channel_id()

class autobahn.asyncio.websocket.WebSocketClientProtocol[source]

Base class for asyncio-based WebSocket client protocols.

Implements:

get_channel_id(channel_id_type=u'tls-unique')[source]

Implements autobahn.wamp.interfaces.ITransport.get_channel_id()

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

Base class for asyncio-based WebSocket server factories.

Implements:

Note

In addition to all arguments to the constructor of autobahn.websocket.interfaces.IWebSocketServerChannelFactory(), you can supply a loop keyword argument to specify the asyncio event loop to be used.

protocol

alias of WebSocketServerProtocol

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

Base class for asyncio-based WebSocket client factories.

Implements:

Note

In addition to all arguments to the constructor of autobahn.websocket.interfaces.IWebSocketClientChannelFactory(), you can supply a loop keyword argument to specify the asyncio event loop to be used.

WAMP-over-WebSocket Protocols and Factories

Classes for WAMP-WebSocket clients and servers using asyncio.

class autobahn.asyncio.websocket.WampWebSocketServerProtocol[source]

asyncio-based WAMP-over-WebSocket server protocol.

Implements:

class autobahn.asyncio.websocket.WampWebSocketClientProtocol[source]

asyncio-based WAMP-over-WebSocket client protocols.

Implements:

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

asyncio-based WAMP-over-WebSocket server factory.

Parameters:
  • factory (callable) – A callable that produces instances that implement autobahn.wamp.interfaces.ITransportHandler
  • serializers (list of objects implementing autobahn.wamp.interfaces.ISerializer) – A list of WAMP serializers to use (or None for all available serializers).
protocol

alias of WampWebSocketServerProtocol

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

asyncio-based WAMP-over-WebSocket client factory.

Parameters:
  • factory (callable) – A callable that produces instances that implement autobahn.wamp.interfaces.ITransportHandler
  • serializer (object implementing autobahn.wamp.interfaces.ISerializer) – The WAMP serializer to use (or None for “best” serializer, chosen as the first serializer available from this list: CBOR, MessagePack, UBJSON, JSON).
protocol

alias of WampWebSocketClientProtocol

WAMP-over-RawSocket Protocols and Factories

Classes for WAMP-RawSocket clients and servers using asyncio.

class autobahn.asyncio.rawsocket.WampRawSocketServerProtocol[source]

asyncio-based WAMP-over-RawSocket server protocol.

Implements:

get_channel_id(channel_id_type=u'tls-unique')[source]

Implements autobahn.wamp.interfaces.ITransport.get_channel_id()

class autobahn.asyncio.rawsocket.WampRawSocketClientProtocol[source]

asyncio-based WAMP-over-RawSocket client protocol.

Implements:

get_channel_id(channel_id_type=u'tls-unique')[source]

Implements autobahn.wamp.interfaces.ITransport.get_channel_id()

class autobahn.asyncio.rawsocket.WampRawSocketServerFactory(factory, serializers=None)[source]

asyncio-based WAMP-over-RawSocket server protocol factory.

Parameters:
  • factory (callable) – A callable that produces instances that implement autobahn.wamp.interfaces.ITransportHandler
  • serializers (list of objects implementing autobahn.wamp.interfaces.ISerializer) – A list of WAMP serializers to use (or None for all available serializers).
protocol

alias of WampRawSocketServerProtocol

class autobahn.asyncio.rawsocket.WampRawSocketClientFactory(factory, serializer=None)[source]

asyncio-based WAMP-over-RawSocket client factory.

Parameters:
  • factory (callable) – A callable that produces instances that implement autobahn.wamp.interfaces.ITransportHandler
  • serializer (object implementing autobahn.wamp.interfaces.ISerializer) – The WAMP serializer to use (or None for “best” serializer, chosen as the first serializer available from this list: CBOR, MessagePack, UBJSON, JSON).
protocol

alias of WampRawSocketClientProtocol

WAMP Sessions

Classes for WAMP sessions using asyncio.

class autobahn.asyncio.wamp.ApplicationSession(config=None)[source]

WAMP application session for asyncio-based applications.

Implements:

Implements autobahn.wamp.interfaces.ISession()

class autobahn.asyncio.wamp.ApplicationRunner(url, realm=None, extra=None, serializers=None, ssl=None, proxy=None, headers=None)[source]

This class is a convenience tool mainly for development and quick hosting of WAMP application components.

It can host a WAMP application component in a WAMP-over-WebSocket client connecting to a WAMP router.

Parameters:
  • url (str) – The WebSocket URL of the WAMP router to connect to (e.g. ws://somehost.com:8090/somepath)
  • realm (str) – The WAMP realm to join the application session to.
  • extra (dict) – Optional extra configuration to forward to the application component.
  • serializers (list) – A list of WAMP serializers to use (or None for default serializers). Serializers must implement autobahn.wamp.interfaces.ISerializer.
  • ssl (ssl.SSLContext or bool) – An (optional) SSL context instance or a bool. See the documentation for the loop.create_connection asyncio method, to which this value is passed as the ssl keyword parameter.
  • proxy (dict or None) – Explicit proxy server to use; a dict with host and port keys
  • headers (dict) – Additional headers to send (only applies to WAMP-over-WebSocket).
run(make, start_loop=True, log_level='info')[source]

Run the application component. Under the hood, this runs the event loop (unless start_loop=False is passed) so won’t return until the program is done.

Parameters:
Returns:

None is returned, unless you specify start_loop=False in which case the coroutine from calling loop.create_connection() is returned. This will yield the (transport, protocol) pair.

stop()[source]

Stop reconnecting, if auto-reconnecting was enabled.