zlib, gzip and HTTP compression.

Neil Schemenauer nas at python.ca
Sat Jan 12 14:23:01 EST 2002


Alan Kennedy wrote:
> I'm going to get to the bottom of this!

It might help to look at the Quixote source.  It supports gzip
compression.  See:

    http://www.mems-exchange.org/software/quixote/

You want to look at the compress_output method in publish.py.  Here is
the main piece of code in case you don't feel like downloading it:

    _gzip_header = ("\037\213" # magic
                    "\010" # compression method
                    "\000" # flags
                    "\000\000\000\000" # time, who cares?
                    "\002"
                    "\377")

    def compress_output (self, request, output):
        import zlib, struct

        encoding = request.get_encoding(["gzip", "x-gzip"])
        if encoding:
            co = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS,
                                  zlib.DEF_MEM_LEVEL, 0)
            chunks = [self._gzip_header,
                      co.compress(output),
                      co.flush(),
                      struct.pack("<ll", zlib.crc32(output), len(output))]
            output = "".join(chunks)
            request.response.set_header("Content-Encoding", encoding)
        return output

HTH,

  Neil




More information about the Python-list mailing list