1

I am trying to do a handshake but i keep getting the error: failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

What am i doing wrong?

server:

import socket
import re
from base64 import b64encode
from hashlib import sha1
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
websocket_answer = (
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}\r\n\r\n',
)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host='10.10.10.12'
port=8999
s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
client_list = []

while True:
    data = conn.recv(1024)
    if not data:
        break
    if(s not in client_list):
        client_list.append(s)
        key = (re.search('Sec-WebSocket-Key:\s+(.*?)[\n\r]+', data.decode('utf-8'))
            .groups()[0]
            .strip())
        print('key' + key)
        response_key = b64encode(sha1((key + GUID).encode('utf-8')).hexdigest().encode('utf-8')).decode('utf-8')
        response = '\r\n'.join(websocket_answer).format(key=response_key)
        print('_________________RESPONSE________________')
        print(response.encode('ascii'))
        conn.send(response.encode('utf-8'))
    else:
        print(str(data))
    print(data.decode('utf-8'))

client:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">
        var s = new WebSocket('ws://10.10.10.12:8999');
        s.onmessage = function(t){console.log(t)};
        s.onopen = function(){
          s.send('hello from client');
        }
    </script>
</head>
<body>
</body>
</html>

https://docs.python.org/2/library/socketserver.html

1
  • Im 45% sure the response_key is incorrect. but the documentation says, "concatenate the key and GUID, hash it with sha1, and base64 encode it". but that is what i have done developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/… . It could also be an encoding problem?? Commented Mar 1, 2016 at 21:39

1 Answer 1

1

This is working for me:

import socket
import re
from base64 import b64encode
from hashlib import sha1
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
websocket_answer = (
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}\r\n\r\n',
)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host='10.10.10.12'
port=8999
s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
#print('Connected by', addr)
client_list = []

while True:
    data = conn.recv(1024)
    if not data:
        break
    if(s not in client_list):
        client_list.append(s)
        key = (re.search('Sec-WebSocket-Key:\s+(.*?)[\n\r]+', data.decode('utf-8'))
            .groups()[0]
            .strip())
        #print('key' + key)
        sha1f = sha1()
        sha1f.update(key.encode('utf-8') + GUID.encode('utf-8'))
        response_key = b64encode(sha1f.digest()).decode('utf-8')
        response = '\r\n'.join(websocket_answer).format(key=response_key)
        #print('_________________RESPONSE________________')
        #print(response)
        conn.send(response.encode('utf-8'))
    else:
          pass
        #print(data.decode('utf-8'))
    print(str(data).encode('utf-8').decode('utf-8'))
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.