Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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//

like image 824
Aleksander Ikleiw Avatar asked Nov 01 '25 08:11

Aleksander Ikleiw


1 Answers

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)
like image 198
Anton Yuzhakov Avatar answered Nov 03 '25 23:11

Anton Yuzhakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!