Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2675

Zlib decompress data from websocket

I am exploring websocket which is sending data encoded in zlib the parameter of endocing is zlib-stream. I wanted to use zlib library but it seems not to work.

import zlib

print(zlib.decompress(text.encode()))

It throws me an error zlib.error: Error -3 while decompressing data: incorrect header check. It is a discord url wss://gateway.discord.gg/?encoding=json&v=8&compress=zlib-stream. This is an example, binary message: wp0KLShOhRZ6FibUSIWgpRPGpuCZaCNqpULQIgFLYNIGlt2UFEcGwyQVAgAAAP//

Upvotes: 4

Views: 2640

Answers (1)

Anton Yuzhakov
Anton Yuzhakov

Reputation: 59

You need to create decompression object and decompress every message from the start. You can't just decompress an arbitrary message.

decompress_obj = zlib.decompressobj()
for compressed_message in messages:
    message = decompress_obj.decompress(compressed_message)

Upvotes: 5

Related Questions