zlib, gzip and HTTP compression.

jj jj at void.si
Fri Jan 11 04:49:31 EST 2002

dont use zlib, 
http requires data in gzip format, zlip.compress returns quite
different structure.
so import gzip ...


On 10 Jan 2002 12:27:42 -0800, alanmk at hotmail.com (Alan Kennedy)

>Greetings all,
>I'm trying to send compressed HTML over a HTTP 1.1 connection, and I
>just cannot get it to work. I'm trying to get it working with
>mod_python, but the same thing happens with CGI, so I've reproduced it
>with a CGI script, to simplify the problem.
>I wonder if anyone might be able to indicate to me what is wrong with
>the CGI code below?
>#! C:/python21/python.exe
>import string
>import os
>import zlib
># Use any old HTML file (which displays fine standalone)
>f = open("test.html")
>buf = f.read()
>zbuf = zlib.compress(buf)
>acceptsGzip = 0
>if string.find(os.environ["HTTP_ACCEPT_ENCODING"], "gzip") != -1:
>	acceptsGzip = 1
>print "Content-type: text/html"
>if acceptsGzip:
>	print "Content-encoding: gzip"
>	print "Content-length: %d" % (len(zbuf))
>	print                                     # end of headers
>	print zbuf                                # and then the buffer
>	print                                     # end of headers
>	print buf                                 # and then the buffer
># End of script
>So far I've tried
>1. Using "Content-encoding: x-gzip"
>2. Using "Transfer-encoding: gzip"
>3. Using "Transfer-encoding: x-gzip"
>4. Setting the "Content-length" to the compressed length of the buffer
>5. Setting the "Content-length" to the uncompressed length of the
>6. All possibilities of case, i.e. "Content-Encoding",
>"CONTENT-ENCODING", "content-encoding", etc (which shouldn't matter,
>it should be case insensitive)
>and all combinations of the above, but to no avail.
>IE5.0, which is a HTTP 1.1 client, displays an empty HTML page with
>the above code. When I use "Content-encoding: x-gzip",
>"Transfer-encoding: gzip" or "Transfer-encoding: x-gzip", IE5.0
>displays the compressed content (i.e. gobbledegook).
>NN4.5, which is a HTTP 1.0 client which accepts gzip encoding, tells
>me "A communications error has occurred. Please Try again".
>It's getting maddening now :-( It should be simple, shouldn't it?
>Is python zlib.compress not compatible with gzip? If not, are there
>are Python library routines that are?
>I'm using Apache 1.3.22 and Pyton 2.1.1
>TIA for any help offered.

More information about the Python-list mailing list