简体   繁体   English

Python:无法解码来自Websocket的服务器响应

[英]Python: Unable to decode response coming from server over websocket

My Python script has created a WebSocket and is able to connect to the server using that WebSocket. 我的Python脚本创建了一个WebSocket,并能够使用该WebSocket连接到服务器。 The server sends a response on the WebSocket that I've created, but the WebSocket module seems to be unable to decode it. 服务器在我创建的WebSocket上发送响应,但是WebSocket模块似乎无法对其进行解码。

This is the error i'm seeing: 这是我看到的错误:

-----------------------
Traceback (most recent call last):
  File "test_file.py", line 307, in <module>
    result = ws.recv()
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 293, in recv
    opcode, data = self.recv_data()
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 310, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 334, in recv_data_frame
    return self.cont_frame.extract(frame)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_abnf.py", line 428, in extract
    "cannot decode: " + repr(frame.data))
websocket._exceptions.WebSocketPayloadException: cannot decode: '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x9dUmk\xeb6\x14\xfe+\xc1\x1f\xc7ug\'v\x1c\x17\xc6\xb8t\xa5\x0b\xac\xbdwm\x06\x83\xb9\x08Y>N\xb5kK\x8e$\'\xcdJ\xff\xfbt$;vac0\x87\x18\xe9\xbc<\xe7\xfd\xf8m\x11\x80RR1YAp\xbd\x88>-\x82\x16\xb4\xa6{\xbc\x05\x9f\xf7 \xccW%_\xcf7\xb2m{\xc1\x195\\\x8a\xc0JqMz\'

This is the piece of code where i'm initiating a websocket and listening to the server resopnse: 这是我启动Websocket并侦听服务器响应的一段代码:

ws = websocket.create_connection(full_url, header = header_set)
result = ws.recv()
print result

It's failing in the ws.recv() line. ws.recv()行中失败。 Any ideas? 有任何想法吗?

This seems to be a known issue with websocket-client version 0.16 . 这似乎是websocket-client版本0.16的已知问题。 Try updating the package to 0.34 , or higher, to see if that resolves the issue. 尝试将软件包更新为0.34或更高版本,以查看是否可以解决问题。

Apparently, this is because websocket-client changed how they handled frame validation in a version after 0.16 (not exactly sure what version though, so try updating to latest). 显然,这是因为websocket-client更改了他们在0.16之后的版本中处理帧验证的方式(虽然不能确切确定是哪个版本,所以请尝试更新到最新版本)。

Supporting link: https://github.com/dpallot/simple-websocket-server/issues/24 支持链接: https//github.com/dpallot/simple-websocket-server/issues/24

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM