Advertisement
Atheuz

Untitled

Jun 4th, 2011
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import urllib2, httplib
  2. import json
  3. import StringIO
  4. import gzip
  5.  
  6. class HandleGZippedJSON:
  7.     def __init__(self, url):
  8.         self.url = url
  9.         self.json_data = None
  10.  
  11.     def run(self):
  12.         #httplib.HTTPConnection.debuglevel = 1
  13.         request = urllib2.Request(self.url)
  14.         request.add_header('Accept-encoding', 'gzip')
  15.         opener = urllib2.build_opener()
  16.         f = opener.open(request)
  17.         c_data = f.read()
  18.         c_stream = StringIO.StringIO(c_data)
  19.         gzipper = gzip.GzipFile(fileobj=c_stream)
  20.         data = gzipper.read()
  21.         self.json_data = json.loads(data)
  22.         #return json.loads(data)
  23.  
  24. def main():
  25.     out = HandleGZippedJSON("http://stackauth.com/sites")
  26.     out.run()
  27.     #print out.json_data
  28.     print json.dumps(out.json_data, indent=4)
  29.  
  30. if __name__ == '__main__':
  31.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement