import gzipimport StringIOdef gzip_compress(buf): out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode="w") as f: f.write(buf) return out.getvalue()def gzip_decompress(buf): obj = StringIO.StringIO(buf) with gzip.GzipFile(fileobj=obj) as f: result = f.read() return result