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 ).

 

 

http://pastebin.com/zBjN02jQ

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 !

About these ads
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:

    Hello,
    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: http://www.tutorialspoint.com/html5/html5_websocket.htm)
    and get error:

    Connection from: (’127.0.0.1′, 56573)
    Exception in thread Thread-1:
    Traceback (most recent call last):
    File “/usr/lib/python2.6/threading.py”, line 532, in __bootstrap_inner
    self.run()
    File “/usr/lib/python2.6/threading.py”, line 484, in run
    self.__target(*self.__args, **self.__kwargs)
    File “server.py”, line 84, in handle_cnxn
    send(u”%s” % (i))
    File “server.py”, line 76, in send
    csock.send(pl)
    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 127.0.0.1 in stead of localhost in the handshake. You also need to type 127.0.0.1:8080 in the address bar.

    You might make note that the draft version 76 expired November 7, 2010
    http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
    and a few drafts have followed since.
    http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-04

    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__.

    Cheers,

    Roger

    • 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 :)

      Cheers,
      Abhinav

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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