There's no different what to use. urllib come with the python and if you don't like to download additional package, use urllib.
As OP mentioned --compressed
in curl command, we should do the same to get gzipped content:
Edited ars
answer:
import sys
if sys.version_info.major == 3:
import urllib.request as urllib2
else:
import urllib2
from StringIO import StringIO
import gzip
request = urllib2.Request('http://example.com/')
request.add_header('Accept-encoding', 'gzip')
request.add_header('Authorization', 'Bearer [ACCESS_TOKEN]')
response = urllib2.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
Does python urllib2 automatically uncompress gzip data fetched from webpage?