使用Python服务器的Websocket握手问题

Jiv*_*ngs 5 python html5 websocket

这是关于Websocket协议76中握手的问题.

我写了一个客户端和服务器,但是让客户端接受握手时遇到了麻烦.我可以看到它被返回,但客户端立即关闭连接.我猜我的md5sum响应一定不正确.

据我所知,我正在遵循正确的程序,谁能告诉我我做错了什么?

def create_handshake_resp(handshake):

  # parse request
  final_line = ""
  lines = handshake.splitlines()
  for line in lines:
    parts = line.partition(":")
    if parts[0] == "Sec-WebSocket-Key1":
      key1 = parts[2]
    elif parts[0] == "Sec-WebSocket-Key2":
      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 "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" % (httphost, sockethost, e.digest())



def parse_key(key):

  spaces = -1
  digits = ""
  for c in key:
    if c == " ":
      spaces += 1
    if is_number(c):
      digits = digits + c


  new_key = int(digits) / spaces
  return str(new_key)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在执行我认为对键的正确操作(按空格数,concat结果和请求的最后一行除以MD5),并且肯定会返回16字节的响应.

任何帮助将不胜感激,一旦我有一份工作副本,我会在这里发布.

谢谢.

编辑:

更改了标题以符合kanaka的回复.握手仍未被客户接受.我发现了如何在Chromium中显示请求,这是给出的请求和响应:

(P) t=1291739663323 [st=3101]     WEB_SOCKET_SEND_REQUEST_HEADERS  
                              --> 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  
                              --> 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
Run Code Online (Sandbox Code Playgroud)

广告逐字,除了我已删除IP地址,原因很明显.

kan*_*aka 5

你有几个问题立刻突然出现在我面前:

  • 你没有正确计算空间.你的计数器应该从0开始,而不是-1.
  • 您的响应标头仍为v75样式.任何以"WebSocket-"(WebSocket-Origin,WebSocket-Location,WebSocket-Protocol)开头的标题应该以v76中的"Sec-WebSocket-"开头.

以下是我如何计算wsproxy中的响应chksum(noVNC和HTML5 VNC客户端的一部分):

import struct, md5
...
spaces1 = key1.count(" ")
spaces2 = key2.count(" ")
num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2

return md5(struct.pack('>II8s', num1, num2, key3)).digest()
Run Code Online (Sandbox Code Playgroud)