0

Trying to figure out what im doing wrong here. I keep getting this error "failed: Error during WebSocket handshake: Sec-WebSocket-Accept mismatch".

Below is what I got from the server:

GET /?encoding=text HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8026
Origin: http://www.websocket.org
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: en04jzbfLcwcFhk5qnUxJg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36

I took "en04jzbfLcwcFhk5qnUxJg==" And add on "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to get the following:

en04jzbfLcwcFhk5qnUxJg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11

So my response is:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: MmJjMDQ2NTc0ZDE3MWVhNjA2ZmE0ZGNhYzQzYjliZmZkNzFmZDk1MQ==

Im not sure what I did wrong but from what I have seen in other examples my websocket accept key seems rather long.

Below is my code any ideas would be helpful.

#!/usr/bin/env python

import socket
import thread
import hashlib
import base64
import binascii

def handshake(conn):
    request = conn.recv(1024).strip()
    print request

    specificationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    websocketKey = ''

    lines = request.splitlines()
    for line in lines:
            args = line.partition(": ")
            if args[0] == 'Sec-WebSocket-Key':
                    websocketKey = args[2]
    print websocketKey

    fullKey = hashlib.sha1(websocketKey + specificationGUID).hexdigest()
    acceptKey = base64.b64encode(fullKey)

    response = 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ' + acceptKey + '\r\n\r\n'
    conn.send(response)
    print response

def accept(conn):
    def threaded():
        while True:
            handshake(conn)

thread.start_new_thread(threaded, ())

if __name__ == "__main__":
    HOST, PORT = 'localhost', 8026
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((HOST, PORT))
    server.listen(5)

    print "Listening on %s" % ("%s:%s" % server.getsockname())

    while 1:
            conn, addr = server.accept()
            accept(conn)
2
  • I found the problem, I used .hexdigest() instead of just using .digest(). changed that and the connection was successful. Commented May 23, 2013 at 20:22
  • answer your question and get yourself some points! Commented May 28, 2013 at 21:05

1 Answer 1

2

I found the problem, I used .hexdigest() instead of just using .digest(). changed that and the connection was successful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.