Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Websocket握手-Websocket握手过程中出错:不正确';Sec WebSocket接受';标题值_Python_Sockets_Http_Websocket - Fatal编程技术网

Python Websocket握手-Websocket握手过程中出错:不正确';Sec WebSocket接受';标题值

Python Websocket握手-Websocket握手过程中出错:不正确';Sec WebSocket接受';标题值,python,sockets,http,websocket,Python,Sockets,Http,Websocket,我正在使用Python的socket来响应Websocket请求,并且我在Sec Websocket Accept头中遇到了问题。以前可能有过类似的问题,但没有人为我工作 from base64 import b64encode from hashlib import sha1 key = b64encode(sha1((key + GUID).encode()).digest()) #This return (b"HTTP/1.1 101 Switching Protocols\

我正在使用Python的socket来响应Websocket请求,并且我在
Sec Websocket Accept
头中遇到了问题。以前可能有过类似的问题,但没有人为我工作

from base64 import b64encode 
from hashlib import sha1

key = b64encode(sha1((key + GUID).encode()).digest()) #This
return (b"HTTP/1.1 101 Switching Protocols\n"
        +b"Content-Type: text/html\n"
        +b"Connection: Upgrade\n"
        +b"Upgrade: websocket\n"
        +b"Sec-WebSocket-Accept: " + key + b"\n" #Maybe this did something?
        +b"\n"
        )    

处的文件规定,对于要建立的连接:

服务器获取握手请求中发送的秒WebSocket密钥的值,附加258EAFA5-E914-47DA-95CA-C5AB0DC85B11,获取新值的SHA-1,然后对其进行base64编码

我当前发送的响应被包装在一个函数中,该函数接收来自请求的密钥 并按照文档中的说明进行操作

def socket101(self,template,key):
    key = key.strip()
    key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" #Add the key
    key = sha1(key.encode()).digest() #Take the sha1
    key = b64encode(key) #B64 encode it
    return (b"HTTP/1.1 101 Switching Protocols\n"
            +b"Content-Type: text/html\n"
            +b"Connection: Upgrade\n"
            +b"Upgrade: websocket\n"
            +"Sec-WebSocket-Accept: {}\n".format(key).encode()
            +b"\n"
            )    
然后我调用上面的方法,如下所示

def AwaitMessage(client,address):
    while True:
        data = client.recv(1024)
        try:
            if str(data.strip()) != '': 
                headers = ParseHeaders(data) #Parses the request headers
                HTTP_MSG = socket101(headers['Sec-WebSocket-Key']) #Call the function above passing the key
                client.send(HTTP_MSG) #Send the message
        except Exception as f:
              pass
    client.close()

但这不希望我做任何事情,并抛出以下错误:

WebSocket握手期间出错:“Sec WebSocket Accept”标题值不正确

浏览器没有提供任何关于它为什么不正确的信息,我猜不出来

JavaScript代码只是与套接字建立连接

const host = window.location.host
const PORT = 8000;
const socket = new WebSocket(`ws://${host}:${PORT}/ws/connection`)

有什么想法吗?

下面的代码适合我

from base64 import b64encode 
from hashlib import sha1

key = b64encode(sha1((key + GUID).encode()).digest()) #This
return (b"HTTP/1.1 101 Switching Protocols\n"
        +b"Content-Type: text/html\n"
        +b"Connection: Upgrade\n"
        +b"Upgrade: websocket\n"
        +b"Sec-WebSocket-Accept: " + key + b"\n" #Maybe this did something?
        +b"\n"
        )    

我几乎可以肯定,这是完全一样的东西,我有上面,但上述不工作


EDIT:问题是
type(key)
bytes
,因此
f'{key}
将在其周围加上引号,但在上面的代码段上,它与其他
字节

相关,您的
Sec-Websocket-Accept
头在您现在添加的末尾缺少一个换行。下一行的换行符表示HTTP头的结束。还请注意,它应该是
\r\n
,而不仅仅是
\n
。许多客户对这一差异持宽容态度,但并非全部。