Auf tempfile hab ich verzichtet, da es unter Windows nicht richtig arbeitet.
Ziel war eigentlich eine benutzung wie bei PHP, was ich bis jetzt noch nicht hinbekommen habe.
Deshalb auch die verwendung eines Destruktors, wo ich es Python überlassen das Object am Ende des scripts zu löschen.
Wems nicht gefällt kann es sich ja umschreiben

Code: Alles auswählen
__version__ = '0.1'
import sys, gzip, time, os
class NotSupportedEncoding(Exception):
pass
class enable:
def __init__(self):
Encodings = os.environ['HTTP_ACCEPT_ENCODING'].split(',')
if 'gzip' in Encodings:
self.TMP_File = "%s.cgigz" % str(time.time())
self.GZIP = gzip.GzipFile(filename=self.TMP_File, mode='wb')
sys.stdout = self.GZIP
else:
raise NotSupportedEncoding
def __del__(self):
sys.stdout = sys.__stdout__
self.GZIP.close()
send = open(self.TMP_File, 'rb')
print send.read()
send.close()
os.remove(self.TMP_File)
Code: Alles auswählen
import cgigz
try:
print "Content-Type: text/html"
print "Content-Encoding: gzip"
print
garbage_collection_dummy = cgigz.enable()
except cgigz.NotSupportedEncoding:
print "Content-Type: text/html"
print
print "hallo"
print "TETFSDHSDGHGHSD"