WebSocket & WAMP for Python on Twisted and asyncio.
Autobahn|Python is a subproject of Autobahn and provides open-source implementations of
for Python 2 and 3, and running on Twisted and asyncio.
You can use Autobahn|Python to create clients and servers in Python speaking just plain WebSocket or WAMP.
WebSocket allows bidirectional real-time messaging on the Web and beyond, while WAMP adds real-time application communication on top of WebSocket.
WAMP provides asynchronous Remote Procedure Calls and Publish & Subscribe for applications in one protocol running over WebSocket. WAMP is a routed protocol, so you need a WAMP Router to connect your Autobahn|Python based clients. We provide Crossbar.io, but there are other options as well.
- framework for WebSocket and WAMP clients and servers
- compatible with Python 2.7 and 3.4 or later (including 3.7)
- runs on CPython, PyPy and Jython
- runs under Twisted and asyncio - implements WebSocket RFC6455 and Draft Hybi-10+
- implements WebSocket compression
- implements WAMP, the Web Application Messaging Protocol
- high-performance, fully asynchronous implementation
- best-in-class standards conformance (100% strict passes with Autobahn Testsuite: Client Server)
- message-, frame- and streaming-APIs for WebSocket
- supports TLS (secure WebSocket) and proxies
- Open-source (MIT license)
Autobahn contains NVX, a network accelerator library that provides SIMD accelerated native vector code for WebSocket (XOR masking) and UTF-8 validation.
Above is for advanced uses. In general we recommend to use CBOR where you can, and JSON (from the standard library) otherwise.
To install Autobahn with all available serializers:
pip install autobahn[serializers]
or (development install)
pip install -e .[serializers]
Further, to speed up JSON on CPython using ujson
, set the environment variable:
AUTOBAHN_USE_UJSON=1
Warning
Using ujson
(on both CPython and PyPy) will break the ability of Autobahn
to transport and translate binary application payloads in WAMP transparently.
This ability depends on features of the regular JSON standard library module
not available on ujson
.
To use cbor2
, an alternative, highly flexible and standards complicant CBOR
implementation, set the environment variable:
AUTOBAHN_USE_CBOR2=1
Note
cbor2
is not used by default, because it is significantly slower currently
in our benchmarking for WAMP message serialization on both CPython and PyPy
compared to cbor
.
To give you a first impression, here are two examples. We have lot more in the repo.
Here is a simple WebSocket Echo Server that will echo back any WebSocket message received:
from autobahn.twisted.websocket import WebSocketServerProtocol
# or: from autobahn.asyncio.websocket import WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {} bytes".format(len(payload)))
else:
print("Text message received: {}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
To actually run above server protocol, you need some lines of boilerplate.
Here is a WAMP Application Component that performs all four types of actions that WAMP provides:
- subscribe to a topic
- publish an event
- register a procedure
- call a procedure
from autobahn.twisted.wamp import ApplicationSession
# or: from autobahn.asyncio.wamp import ApplicationSession
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
# 1. subscribe to a topic so we receive events
def onevent(msg):
print("Got event: {}".format(msg))
yield self.subscribe(onevent, 'com.myapp.hello')
# 2. publish an event to a topic
self.publish('com.myapp.hello', 'Hello, world!')
# 3. register a procedure for remote calling
def add2(x, y):
return x + y
self.register(add2, 'com.myapp.add2')
# 4. call a remote procedure
res = yield self.call('com.myapp.add2', 2, 3)
print("Got result: {}".format(res))
Above code will work on Twisted and asyncio by changing a single line
(the base class of MyComponent
). To actually run above application component, you need some lines of boilerplate and a WAMP Router.