簡體   English   中英

無法解壓 web-socket 數據

[英]Trouble Decompressing web-socket data

我想從 Bitmart 的 WebSocket(交易所)獲取數據。 我能夠訂閱 WebSocket 並取回數據,但它是壓縮的,根據 文檔我應該使用 zlib 解壓縮數據,但是當我嘗試這樣做時,它給出了一個錯誤: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 1: invalid continuation byte

這是我的代碼:

import json
from websocket import create_connection
import zlib

ws = create_connection("wss://ws-manager-compress.bitmart.news?protocol=1.1")
ws.send(json.dumps({
    "op": "subscribe",
    "args": ["spot/ticker:BTC_USDT"]
}))

while True:
    result = ws.recv()
    message = result
    compressed = zlib.compress(message)
    decompressed = zlib.decompress(compressed).decode('UTF-8')
    print(decompressed)

ws.close()

BTW ws.recv()返回如下數據:

b'5\xcd\xd1\x0e\x82 \x18\x05\xe0w\xf9\xaf\x1d\x01\x82\xbfzY\xbdAv\xd5\x1aCc\xe9\xc2pB\xb5\xe6|\xf7`\xcb\xdb\xef\x9c\x9d\xb3\xc0M\x07\r\xf5e\x81V{\xa3\xde\xce\xbeF\xa3\xb8\xe8\xa1\x06N\xab\x1c\x11+\x82\x122\xe8\x87{\xff\x0fdI)%\x94F\xb5\xda\x075\xcdCg\x92#2I\x10\x93\xbb\xcfV\x96L\xe4\xa4,"\xba\xc9<7\xc5\x9cK\xc2\xd2\x84W\x01jVp*\xa8(\xa5\x8c\xf0\x1d[gci\xdf\x1c\xd4\xf9tl`\xbdf\x10tk\xd3\x89\x9f\\\xd8\x85\xa1{\x98\x19\xd6\x1f'

解壓縮 = zlib.decompress(message, -zlib.MAX_WBITS).decode('UTF-8')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM