nick34992
2/17/2017 - 11:38 PM

Python Modules: urllib: request, urlopen and gzip

Python Modules: urllib: request, urlopen and gzip

#!/usr/bin/env python3
'''
Request debian.org homepage. Add header Accept-Encoding:gzip to request
compressed file. Decompress file. Print using for loop while decoding
to utf-8.
'''

from urllib.request import Request, urlopen
import gzip

req = Request('http://www.debian.org')
req.add_header('Accept-Encoding', 'gzip')
response = urlopen(req)
content = gzip.decompress(response.read())
decomp_req = content.splitlines()
for line in decomp_req:
    print(line.decode('utf-8'))