简体   繁体   English

无法解码来自客户端的握手请求

[英]Can't decode handshake request from client

I'm trying to connect a python3 socket with a JavaScript WebSocket.我正在尝试使用 JavaScript WebSocket 连接 python3 套接字。 I'm testing the server code on localhost:8080, and connecting with Chrome(94.0).我正在 localhost:8080 上测试服务器代码,并与 Chrome(94.0) 连接。

The WebSocket client and the socket server do establish a connection. WebSocket 客户端和套接字服务器确实建立了连接。 However, the connection would be shut down if the server did not follow the protocol and respond to the handshake.但是,如果服务器不遵循协议并响应握手,则连接将被关闭。 According to the protocol, the client WebSocket sends a utf-8 encoded GET request to the socket server, and the server must parse that request in order to complete the handshake.根据协议,客户端 WebSocket 向套接字服务器发送一个 utf-8 编码的 GET 请求,服务器必须解析该请求才能完成握手。

What I get from the client is this:我从客户那里得到的是:

\x16\x03\x01\x02\x00\x01\x00\x01\xfc\x03\x03\x97t\xb1\xf7\xac\x99\x91\xd4\xf5\n\xdf\xc3X\x8af<\xbe\x99(0\x88\x9a!\xc6\xc9\x17]\xfe\xd9sP\xcc \xa8\xcf\x90{\x8a\xfcm\xbcj-5\xdf\xf90\x81\xc8Y\xc1\x85q"\xfe!C\xbb\t\xbd}\xe0\x8d\xf1\xe5\x00 \x1a\x1a\x13\x01\x13\x02\x13\x03\xc0+\xc0/\xc0,\xc00\xcc\xa9\xcc\xa8\xc0\x13\xc0\x14\x00\x9c\x00\x9d\x00/\x005\x01\x00\x01\x93\x1a\x1a\x00\x00\x00\x17\x00\x00\xff\x01\x00\x01\x00\x00\n\x00\n\x00\x08\xfa\xfa\x00\x1d\x00\x17\x00\x18\x00\x0b\x00\x02\x01\x00\x00#\x00\x00\x00\x05\x00\x05\x01\x00\x00\x00\x00\x00\r\x00\x12\x00\x10\x04\x03\x08\x04\x04\x01\x05\x03\x08\x05\x05\x01\x08\x06\x06\x01\x00\x12\x00\x00\x003\x00+\x00)\xfa\xfa\x00\x01\x00\x00\x1d\x00 \xfa)\xd5\x85<\x81.\x7f\xc0\x87wA!\xf1\xc9\xf80\xeb\x01\xdc\xed3m\xd1\x98\xd6\xd8\xd7\x7f#\rM\x00-\x00\x02\x01\x01\x00+\x00\x0b\n\x8a\x8a\x03\x04\x03\x03\x03\x02\x03\x01\x00\x1b\x00\x03\x02\x00\x02JJ\x00\x01\x00\x00\x15\x00\xf7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

The client consistently sends these bytes, although it this does not look like a legitimate GET request.客户端始终发送这些字节,尽管这看起来不像一个合法的 GET 请求。 This can't be decoded by utf-8 or by other encodings including utf-16-le, utf-16-be, unicode-escape, etc.这无法通过 utf-8 或其他编码(包括 utf-16-le、utf-16-be、unicode-escape 等)进行解码。

client source:客户来源:

var client = new WebSocket('wss://127.0.0.1:8080')

client.onopen = function(e) {
    console.log("open");
}

client.onmessage = function(e) {
    console.log(e.data)
}

server handshake handler:服务器握手处理程序:

def handshake(client, address):
    req = client.recv(1024).decode('utf-8')
    key = (re.search('Sec-WebSocket-Key:\s+(.*?)[\n\r]+', req).groups()[0].strip())
    response_key = b64encode(sha1(key + sockey).digest())
    response = '\r\n'.join(websock_ans).format(key=response_key)
    client.send(response.encode('utf-8'))

As the client request isn't utf-8 bytes, this gives a decoding error in line 2.由于客户端请求不是 utf-8 字节,这在第 2 行给出了解码错误。

following the websocket protocol ( https://datatracker.ietf.org/doc/html/rfc6455 ), sockey and websock_ans are defined as follows:遵循 websocket 协议( https://datatracker.ietf.org/doc/html/rfc6455),sockey和 websock_ans 定义如下:

sockey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
websock_ans = {
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}\r\n\r\n',
}

Does WebSocket compress/encrypt its GET requests? WebSocket 是否压缩/加密其 GET 请求? As far as I checked, the documents do not state any compression/encryption algorithms.据我检查,这些文件没有说明任何压缩/加密算法。

Thanks in advance.提前致谢。

使用gzip将字节转换为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在客户端无法获得握手错误消息 - Can't get the handshake error message in the client Javascript websocket客户端库,可以在握手请求中传递自定义标头 - Javascript websocket client library which can pass custom headers in handshake request 无法使用 React 客户端发出 API 请求 - Can't make an API request with React client 为什么我在WebSocket握手请求标头中看不到cookie值? - Why can't I see cookie value in a WebSocket handshake request header? 无法解码Python中Javascript的JSON响应 - Can't decode JSON response from Javascript in Python 无法从文件json_decode()-语法错误 - Can't json_decode() from file - Syntax error React-使用axios的Nodejs / Express获取请求-无法从客户端访问我的后端路由-有什么错误? - React - Nodejs / express get request using axios - can't hit my backend route from client - what is the mistake? 服务器无法识别REST服务中来自客户端的获取请求 - Server won't recognise get request from client in REST service 从python中的HTTP POST请求解码图像 - Decode image from HTTP POST request in python 如何将POST请求中的JSON发送给客户端? - How can I send the JSON coming from a POST request to the client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM