import gzip
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode='w') as f:
    f.write("This is mike number one, isn't this a lot of fun?")
out.getvalue()
How do I gzip compress a string in Python?