HTML5 WebSocket Server – Work in Progress

So, I thought I’d devise a websocket server in Python. I’ve been tweeting some stuff about that too, but as soon as I wanted to publish the source code, I accidentally deleted, instead of websocket.pyc (which I didn’t want to occur on github). That was quite dumb…

A basis for further development

Today I started rewriting it from scratch. Unfortunately, the “draft76” implementation I wrote back then is already outdated. So the code isn’t really all that useful, except that I got a basic version working, though it still blocks threads that have a client connected, and the server won’t shutdown until the clients disconnect. That is a design error and a major bug that demands yet another rewrite.

Anyway, the part that cost me the most time was figuring out the opening handshake code, so here it is:

def _challenge_response(self, key1, key2, key3):
    """The response to a WebSocket challenge. 
    - key1 is the contents of the Sec-WebSocket-Key1 header
    - key1 is the contents of the Sec-WebSocket-Key2 header
    - key3 is the contents of the 8 bytes long request body"""
    def extract_number(key):
        number = ''
        spaces = 0
        for ch in key:
            if ch == " ": 
                spaces += 1
            elif ch.isdigit(): 
                number += ch
        number = int(number)
        if spaces > 0 and number > 0 and number % spaces == 0:
            return int(int(number) / spaces)
        raise ValueError("Invalid challenge part %s, can not extract number" % key)
    self._debug("Challenge: \n\t%s\n\t%s\n\t%s" % (key1, key2, key3))
    packed = pack('>L', extract_number(key1))
    packed += pack('>L', extract_number(key2)) 
    packed += key3
    response =  hashlib.md5(packed).digest()
    self._debug("Response: %s" % response)
    return response

Current development code

Just to be sure I won’t delete the source code again, I just published the development code on my github.

This entry was posted in Development and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Surrey
    Posted July 1, 2011 at 23:14 | Permalink

    You really ought to put *.pyc in your .gitignore file. This way you wouldn’t have to worry about deleting your .pyc files every time you wanted to push to Github.

    • drm
      Posted July 17, 2011 at 12:03 | Permalink

      That was the plan, but I deleted them before I added the sources to git 🙂 Thanks for the tip though

  2. Posted April 19, 2014 at 13:46 | Permalink

    Pretty great post. I simply stumbled upon your blog and wanted to mention that I have truly enjoyed browsing your weblog posts. In any case I’ll be subscribing on your rss feed and I am hoping you write once more very soon!

  3. Posted April 21, 2014 at 07:17 | Permalink

    I always used to study post in news papers but now as I am a user of web therefore from now I am using net for articles, thanks to web.

  4. Posted April 24, 2014 at 19:12 | Permalink

    Sagging hinges will cause you to have to slam the doors and often more than once to fasten them. He also added that 80% corollas are driven among the 7000,000 vehicles. To buy used cars in hyderabad like used maruti cars on afgfordable price visit free classifieds site khojle.

  5. Posted November 30, 2016 at 07:43 | Permalink

    I have read so many posts on the topic of the blogger lovers but this article is actually a nice article, keep it up.

    Feel free to surf to myy web site: [NordicTrack elliptical with ifit]( “NordicTrack elliptical with ifit”)

  6. Posted January 13, 2017 at 12:40 | Permalink

    Fantastic beat ! I wish to apprentice at the same time as you amend your website, how can i subscribe for a weblog web site? The account aided me a appropriate deal. I have been tiny bit acquainted of this your broadcast offered bright clear idea

    My web blog; [vw camper interiors]( “vw camper interiors”)

  7. Posted October 3, 2018 at 14:41 | Permalink

    ??????????? ????? ?????? ?? ????????? ??????? ????????? ? ??????? ????????????? ??? ??????????? ????????? ???????, ????? ???????? ??? ?????.

    Here is my page ???????? ?????? ?????? ? ????????? ([]( “”))

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Subscribe without commenting