Zlib decompress data from websocket

Viewed 1175

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 urlwss://gateway.discord.gg/?encoding=json&v=8&compress=zlib-stream This is an example ,,binary message'' wp0KLShOhRZ6FibUSIWgpRPGpuCZaCNqpULQIgFLYNIGlt2UFEcGwyQVAgAAAP//

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)
Related