-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuncompress.py
More file actions
23 lines (20 loc) · 789 Bytes
/
uncompress.py
File metadata and controls
23 lines (20 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# _*_coding:utf8_*_
from StringIO import StringIO
import urllib2
import gzip
class ContentEncodingProcessor(urllib2.BaseHandler):
"""A handler to add gzip capabilities to urllib2 requests """
def uncompress_gzip(self, url):
""" This checks if the content is gzipped and decompresses it. """
request = urllib2.Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
print "the page is compressed."
buf = StringIO(response.read())
f = gzip.GzipFile(fileobj = buf)
data = f.read()
return data
else:
print "the page is uncompressed."
return response.read()