Python 处理server返回gzip内容

Python 如何处理server返回gzip压缩过的内容,代码如下:

from StringIO import StringIOimport gzip

request = urllib2.Request('http://outofmemory.cn/')
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)if response.info().get('Content-Encoding') == 'gzip':
    buf = StringIO( response.read())
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()
posted on 2013-12-08 20:58  强巴拉  阅读(889)  评论(0编辑  收藏  举报