Search code examples
pythonpython-2.7gzippacketscapy

Decompressing gzip packets with python


I'm creating a PCAP parser and analyzer in Python with the Scapy package but I'm running into an issue where just about everything is compressed with gzip. Is there a way for me to have gzipped packet payloads decompressed as my program loops through the PCAP file?

In the packet payload there is this line:

Accept-Encoding: gzip,deflate

What does that mean?


Solution

  • You can use the gzip module to uncompress the data

    import StringIO
    import gzip
    
    compressed_data = f.read()
    compressed_stream = StringIO.StringIO(compressed_data)
    gzipper = gzip.GzipFile(fileobj=compressed_stream)
    data = gzipper.read()
    

    Like explained in http://www.diveintopython.net/http_web_services/gzip_compression.html