This short intro will get you started with Autobahn|Python in no time:
Further, Autobahn|Python requires a networking library and currently supports:
Asyncio comes bundled with CPython 3.4+ and can be installed from here on CPython 3.3.
Twisted runs best on Python 2 (support for Python 3 is not yet fully complete), but the bits necessary for WebSocket already work on Python 3. For Twisted installation, please follow the instructions on the Twisted website.
easy_install -U autobahn
Source code is available from Git repository. To install from sources:
git clone git://github.com/tavendo/AutobahnPython.git cd AutobahnPython git checkout v0.8.2 cd autobahn python setup.py install
We are using tags to mark release versions, so just take the latest of these (list tags with "git tag"). Do NOT use the current HEAD (master branch), since this is where development takes place, and it might be broken.
When you don't have Git on your machine, you can download a source code archive package (tar.gz/zip) from the GitHub project page.
To check whether installation was indeed successful:
$ python Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import autobahn >>> autobahn.version '0.7.0'
Lets fire up a simple WebSocket echo server and test it from a browser.
If you are using Asyncio, put the following code into a myserver_asyncio.py file:
from autobahn.asyncio.websocket import WebSocketServerProtocol, \ WebSocketServerFactory class MyServerProtocol(WebSocketServerProtocol): def onMessage(self, payload, isBinary): self.sendMessage(payload, isBinary) if __name__ == '__main__': import asyncio factory = WebSocketServerFactory("ws://localhost:9000", debug = False) factory.protocol = MyServerProtocol loop = asyncio.get_event_loop() coro = loop.create_server(factory, '127.0.0.1', 9000) server = loop.run_until_complete(coro) try: loop.run_forever() except KeyboardInterrupt: pass finally: server.close() loop.close()
and start your server by doing
If you are using Twisted, put the following code into a myserver_twisted.py file:
from autobahn.twisted.websocket import WebSocketServerProtocol, \ WebSocketServerFactory class MyServerProtocol(WebSocketServerProtocol): def onMessage(self, payload, isBinary): self.sendMessage(payload, isBinary) if __name__ == '__main__': import sys from twisted.python import log from twisted.internet import reactor log.startLogging(sys.stdout) factory = WebSocketServerFactory("ws://localhost:9000", debug = False) factory.protocol = MyServerProtocol reactor.listenTCP(9000, factory) reactor.run()
and start your server by doing
Put the following code into a myclient.html file and open the file in a browser: xxx
To see logging output, open your browser debugging console (e.g. with Chrome or Firefox on Windows, press F12). Click the button to send a message and watch the echo'ed messages in the console.
Use a current version of Chrome, Firefox, Safari or IE10+. Support for older browsers is there, but things need to be prepared for that.
If you want to learn more about making your AutobahnPython servers play with browser clients, check out the companion project AutobahnJS.
When you want to access your AutobahnPython servers from non-browser clients, we develop native Autobahn client libraries for that too. Have a look at whats already there, and whats in the queue on the Autobahn project home page.
Finally, if you run into problems, have questions or want to contribute, don't hesitate to join us!