Python WebSockets Server with circuits

Today marks the release of circuits-1.5 (1) which brings to the circuits (2) application framework a new circuits.web dispatcher.

The WebSockets dispatcher.

This dispatcher allows you to build a web application that is capable of accepting WebSockets client connections on the same process/thread.

Using circuits-1.5 here is a very simple Web Application that has WebSockets support and echos back every message it recieves:


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" xml:lang="en-US" lang= "en-US"> <head> <title>circuits.web WebSockets Demo</title> <meta http-equiv="content-type" content= "application/xml+xhtml; charset=us-ascii" /> </head>
<body> //<![CDATA[ function test() { var ws = new WebSocket("ws://" + + "/websocket"); ws.onopen = function(e) { console.log("Opened"); console.log(e); ws.send("Hello World!"); }; ws.onerror = function (e) { console.log("Error"); console.log(e); }; ws.onmessage = function (e) { console.log("Message"); console.log(; }; ws.onclose = function(e) { console.log("Closed"); console.log(e); }; return ws; } //]]> </script> </body> </html>

#!/usr/bin/env python

from import Write from circuits.web import Logger, Server, Static from circuits.web.dispatchers import WebSockets from circuits import handler, BaseComponent, Component, Event
from circuits import Debugger
class Echo(Component):
channel = "ws"
def message(self, sock, data): self.push(Write(sock, data))
(Server(("", 8000)) + WebSockets("/websocket") + Debugger() + Static() + Logger() + Echo() ).run()

Try running this example in Chrome. Bring up the Developer Tools -> Console and type into the console the following:

ws = test();
ws.send("Hello World!");

Have fun!




Sorry comments are normally enabled but have been disabled because sapmemrs are stupid idiots who love to abuse the openness of teh web. There will be no more comments on these old blogs. Sorry. Please refer instead fo my latest blog entries over at

--JamesMills / prologic