title: Websocket handshake problem using Python server
body: <p>This is a question regarding handshaking in <a href="https://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-76" rel="nofollow noreferrer">Websocket Protocol 76</a>.</p>
  <p>I have written a client and server, but am having trouble getting the client
  to accept the handshake. I can see that it is being returned, but the client immediately closes the connection. I'm guessing that my md5sum response must be incorrect.</p>
  <p>As far as I can tell, I'm following the proper procedure, can anyone tell me what I am doing wrong?</p>
  <pre><code>def create_handshake_resp(handshake):
  
    # parse request
    final_line = &quot;&quot;
    lines = handshake.splitlines()
    for line in lines:
      parts = line.partition(&quot;:&quot;)
      if parts[0] == &quot;Sec-WebSocket-Key1&quot;:
        key1 = parts[2]
      elif parts[0] == &quot;Sec-WebSocket-Key2&quot;:
        key2 = parts[2]
      final_line = line
  
    #concat the keys and encrypt
    e = hashlib.md5()
    e.update(parse_key(key1))
    e.update(parse_key(key2))
    e.update(final_line)
    return &quot;HTTP/1.1 101 WebSocket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection:     Upgrade\r\nWebSocket-Origin: http://%s\r\nWebSocket-Location: ws://%s/\r\nWebSocket-Protocol: sample\r\n\r\n%s&quot; % (httphost, sockethost, e.digest())
  
  
  
  def parse_key(key):
  
    spaces = -1
    digits = &quot;&quot;
    for c in key:
      if c == &quot; &quot;:
        spaces += 1
      if is_number(c):
        digits = digits + c
  
  
    new_key = int(digits) / spaces
    return str(new_key)
  </code></pre>
  <p>As you can see, I am performing what I think to be the correct operations on the keys (divide numbers by space count, concat results and the last line of the request and then MD5) and a 16 byte response is definitely being returned.</p>
  <p>Any help would be much appreciated, and as soon as I have a working copy I will post it here.</p>
  <p>Thanks.</p>
  <p>EDIT:</p>
  <p>Changed the headers to comply with kanaka's response. Handshake is still not being accepted by the client.
  I found out how to display the requests in Chromium, and this is the request and response being given:</p>
  <pre><code>(P) t=1291739663323 [st=3101]     WEB_SOCKET_SEND_REQUEST_HEADERS  
                                --&gt; GET / HTTP/1.1   
                                    Upgrade: WebSocket
                                    Connection: Upgrade
                                    Host: ---
                                    Origin: http://---
                                    Sec-WebSocket-Key1: 3E 203C 220 642;
                                    Sec-WebSocket-Key2: Lg 590 ~5 703O G7  =%t 9
                                                     
                                    \x74\x66\xef\xab\x50\x60\x35\xc6\x0a
  (P) t=1291739663324 [st=3102]     SOCKET_STREAM_SENT     
  (P) t=1291739663348 [st=3126]     SOCKET_STREAM_RECEIVED  
  (P) t=1291739663348 [st=3126]     WEB_SOCKET_READ_RESPONSE_HEADERS  
                                --&gt; HTTP/1.1 101 WebSocket Protocol Handshake
                                    Upgrade: WebSocket
                                    Connection: Upgrade
                                    Sec-WebSocket-Origin: http://---
                                    Sec-WebSocket-Location: ws://---/
                                    Sec-WebSocket-Protocol: sample
                                                     
                                    \xe7\x6f\xb9\xcf\xae\x70\x57\x43\xc6\x20\x85\xe7\x39\x2e\x83\xec\x0
  </code></pre>
  <p>Ad verbatim, except I've removed the IP address for obvious reasons.</p>