Python 为什么websocket握手失败?

Python 为什么websocket握手失败?,python,websocket,Python,Websocket,我有以下代码供python服务器创建并发回握手响应 def HandShake(self, request): specificationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" websocketkey = "" protocol = "" for line in request.split("\n"): if "Sec-WebSocket-Key:" in line:

我有以下代码供python服务器创建并发回握手响应

    def HandShake(self, request):
    specificationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    websocketkey = ""
    protocol = ""
    for line in request.split("\n"):
        if "Sec-WebSocket-Key:" in line:
            websocketkey = line.split(" ")[1]
        elif "Sec-WebSocket-Protocol" in line:
            protocol = line.split(":")[1].strip()

    print("websocketkey: " + websocketkey + "\n")
    fullKey = hashlib.sha1(websocketkey.encode("utf-8") + specificationGUID.encode("utf-8")).digest()
    acceptKey = base64.b64encode(fullKey)
    print("acceptKey: " + str(acceptKey, "utf-8") + "\n")
    if protocol != "":
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\nSec-WebSocket-Protocol: " + protocol + "\r\n\r\n"
    else:
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\n\r\n"
    print(handshake)
    self.request.send(bytes(handshake, "utf-8"))
server started, waiting for connections...
request:
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:600
Origin: http://localhost
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: yLffHPqMU4gIW2WnKq+4BQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36


websocketkey: yLffHPqMU4gIW2WnKq+4BQ==

acceptKey: A0eCd19URtkji0OPV162okWsCns=

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: A0eCd19URtkji0OPV162okWsCns=
我已经用维基百科上的例子测试了我计算密钥的方法,所以我知道这是正确的。但是,每次尝试连接到服务器时,我都会遇到以下错误:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch
我不明白我在这里做错了什么。有人知道出了什么问题吗

编辑:打印输出的示例,打印原始消息和伪造的握手

    def HandShake(self, request):
    specificationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    websocketkey = ""
    protocol = ""
    for line in request.split("\n"):
        if "Sec-WebSocket-Key:" in line:
            websocketkey = line.split(" ")[1]
        elif "Sec-WebSocket-Protocol" in line:
            protocol = line.split(":")[1].strip()

    print("websocketkey: " + websocketkey + "\n")
    fullKey = hashlib.sha1(websocketkey.encode("utf-8") + specificationGUID.encode("utf-8")).digest()
    acceptKey = base64.b64encode(fullKey)
    print("acceptKey: " + str(acceptKey, "utf-8") + "\n")
    if protocol != "":
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\nSec-WebSocket-Protocol: " + protocol + "\r\n\r\n"
    else:
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\n\r\n"
    print(handshake)
    self.request.send(bytes(handshake, "utf-8"))
server started, waiting for connections...
request:
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:600
Origin: http://localhost
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: yLffHPqMU4gIW2WnKq+4BQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36


websocketkey: yLffHPqMU4gIW2WnKq+4BQ==

acceptKey: A0eCd19URtkji0OPV162okWsCns=

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

您的代码中有一个bug

用于请求中的行。拆分(“\n”):
如果“Sec WebSocket Key:”在同一行:
websocketkey=line.split(“”[1]
这将返回Sec WebSocket密钥中的
“\r”

证据:

正常RFC行为

客户端密钥:“yLffHPqMU4gIW2WnKq+4BQ=”
服务器密钥:YVjKqlMRxlzzM70LScN9VoCsboI=
不良行为

客户端密钥:“YLFHPQMU4GIW2WNKQ+4BQ==\r”
服务器密钥:A0eCd19URtkji0OPV162okWsCns=

链接到和。

发布您的问题中的
print()
调用的输出。我用打印输出编辑了op