1

I'm having an hard time converting the output of a websocket connection to regular string and later to json.

Here is what a simple message looks like:

msg = await ws.recv()
print(msg)

    b'\xabVJ-K\xcd+Q\xb2R*.M*N.\xcaLJU\xd2QJ\xceH\xcc\xcbK\xcd\x01\x89\x16\xe4\x97\xe8\x97d&g\xa7\x16Y\xb9\x86x\xe8\x86\x06\xbb\x84(\xd5\x02\x00'

I tried the following: print(str(msg, 'utf-8')) But i got the following error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xab in position 0: invalid start byte

The same happens if i try .decode("utf-8") Can anyone help me find what i'm doing wrong here? Thanks in advance!

JayK23
  • 287
  • 1
  • 15
  • 49
  • Does this answer your question? [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – Brian61354270 Oct 06 '20 at 20:32
  • Are you sure the message is a string? It might be a binary message. – Barmar Oct 06 '20 at 20:32
  • @Brian I tried to use .decode("utf-8"), but i'll still get the error – JayK23 Oct 06 '20 at 20:33
  • @Barmar print(type(msg)) will give me a bytes type – JayK23 Oct 06 '20 at 20:34
  • The issue is how the bytes are intended to be interpreted. Some parts are likely to be binary information, other parts may be text strings. You need to extract the different parts and process them appropriately. – Barmar Oct 06 '20 at 20:35
  • You need to check the application protocol specification to see how to parse the message. – Barmar Oct 06 '20 at 20:36
  • 1
    Only thing i found on the docs is the following, but i don't know if it has to do with my problem: "All the messages returning from WebSocket API are optimized by Deflate compression. Users are expected to decompress the messages by their own means(Compression and decompression through the inflate algorithm)." – JayK23 Oct 06 '20 at 20:38
  • Full docs: https://www.okex.com/docs/en/#ws_swap-README – JayK23 Oct 06 '20 at 20:44

1 Answers1

1

The messages are optimized by Deflate compression. All i had to do was to inflate the message:

async def inflate(data):
    decompress = zlib.decompressobj(-zlib.MAX_WBITS)  # see above
    inflated = decompress.decompress(data)
    inflated += decompress.flush()
    return inflated

I found the code here: https://github.com/chopinvan/The-Unofficial-OKEX-Websocket-Bot/blob/master/okex_websocket.py

JayK23
  • 287
  • 1
  • 15
  • 49