How to get number of compressed bytes from GzipFile

Matt matt at vazor.com
Fri Mar 14 18:43:31 EDT 2008


Hi all,

I'm using the gzip module to return a gzip response from a small
python httpserver.  I'd like to know the number of bytes written to
the underlying socket, but it doesn't seem to support the tell()
function.  This works fine for a file:

[15:39:51] mattb ~ $ cat mygzip.py
#!/usr/bin/env python

import os
import gzip

f = open("tmp.gz", "wb")
gz = gzip.GzipFile('', 'wb', 6, f)

for i in xrange(100):
    gz.write('abcdefg' * 100)

gz.flush()

print gz.tell()
print f.tell()

gz.close()
f.close()

print os.stat('tmp.gz').st_size
[15:40:17] mattb ~ $ ./mygzip.py
70000
141
151

So I wrote 70000 raw bytes which gets compressed to 151 bytes -- I
guess the 10-byte difference is a gzip header or something?

Is there any way to get this same functionality when using a socket?

thx

Matt



More information about the Python-list mailing list