Python websocket server ( websocket protocol 76 )

*Note* – As of Oct 24, 2011, This version of websocket server will only work properly with Google Chrome <= 13.X. The new and last call ( hopefully stable) version of the websocket draft has been released, and soon someone will implement the handshake, which changes a bit ( the headers change a little, and so do the framing/encoding bits ).

A simple python server that handshakes an HTML5 enabled browser connecting to it using websockets. Also includes basic message framing. pywebsocket was way too overkill for what I needed, and there were no other python implementations implementing revision 76 (most implement 75 which is slightly different) so I decided to implement a quick handshake myself. The server’s end of the handshake has 13 steps. If you think that’s too much, you should know that the client’s side has 43 steps !

TODO : disconnect handshake ( doesn’t interrupt functionality though ).

Websockets mark the death of workarounds like ajax, orbited/comed, polling, since a browser can now simply open a socket and connect to any application with an open socket that is willing to handshake it according to the w3c’s recommendations.

Enjoy !

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

6 Responses to Python websocket server ( websocket protocol 76 )

  1. Pingback: Erlang websocket server (handshake draft 76 ) « Dream Child

  2. vy says:

    Thanks for sharing, but for my don’t work.
    I am run this server – work, but if i am trying connect(i am for example client use this:
    and get error:

    Connection from: (‘’, 56573)
    Exception in thread Thread-1:
    Traceback (most recent call last):
    File “/usr/lib/python2.6/”, line 532, in __bootstrap_inner
    File “/usr/lib/python2.6/”, line 484, in run
    self.__target(*self.__args, **self.__kwargs)
    File “”, line 84, in handle_cnxn
    send(u”%s” % (i))
    File “”, line 76, in send
    error: [Errno 32] Broken pipe

    maybe know how fix this?

    Thanks, Good Luck!

  3. anonymouscowherd says:

    Broken Pipe basically means the other end of the socket ( your client ) has closed the socket, and the server is trying to write to the socket after this, which causes the error.

    You need to find out why the other side ( the client/browser ) is closing your connection. You can see what is happening through
    Tools -> Developer Tools -> Console in your chrome browser.

    There can be several reasons :

    1. You are not specifying the proper URL. e.g:, in this case, it should be :
    var ws = new WebSocket(“ws://localhost:8000/”);

    2. Your browser is using another handshake version instead of 76. ( probably 75 – in which case you will need to upgrade your chrome browser ).

    Whatever is happening, will be visible once you go to
    Tools -> Developer Tools -> Console

    by clicking on the small settings icon located at the right end of the address bar in chrome.

    Have fun !

  4. Roger Erens says:

    anonymouscowherd :
    1. You are not specifying the proper URL. e.g:, in this case, it should be :
    var ws = new WebSocket(“ws://localhost:8000/”);

    In the server you use the notation in stead of localhost in the handshake. You also need to type in the address bar.

    You might make note that the draft version 76 expired November 7, 2010
    and a few drafts have followed since.

    I don’t know how to find out which draft is supported by which version of which browser.

    Finally, some typo: in the code you should put quotes around __main__.



    • anonymouscowherd says:

      Thanks !

      Corrected that __main__ typo :) . Also thanks for the info about the draft expiring :) Although, the code still works for my chrome browser – tried it just now :)

      Yeah, looks like there is no way to know which draft the browser supports, except going through the source code which is a hassle at the moment :)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s