I’m playing around with the Stack Overflow API using Python. I’m trying to decode the gzipped responses that the API gives.
import urllib, gzip
url = urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')
gzip.GzipFile(fileobj=url).read()
According to the urllib2 documentation, urlopen
“returns a file-like object”.
However, when I run read()
on the GzipFile object I’ve created using it, I get this error:
AttributeError: addinfourl instance has no attribute 'tell'
As far as I can tell, this is coming from the object returned by urlopen
.
It doesn’t appear to have seek either, as I get an error when I do this:
url.read()
url.seek(0)
What exactly is this object, and how do I create a functioning GzipFile
instance from it?
Content-Encoding: gzip
should be handled by the http library, but unfortunately it isn't. This is issue 9500 in Python's bug database, for the interested. – Defloration