Python mechanize gzip response handling

Firstly, if you are not using SSL for all your sites, you should step it up! NameCheap's SSL certificates start at only $9. There's no excuse!
Firstly, if you have not tried Digital Ocean's SSD Virtual Servers for only $5/mo, I highly recommend them!

Mechanize is awesome. The documentation is shit. The gzip support is non-existent. Some sites like Yahoo! require gzip support.

def ungzipResponse(r,b):
	headers =
	if headers['Content-Encoding']=='gzip':
		import gzip
		gz = gzip.GzipFile(fileobj=r, mode='rb')
		html =
		headers["Content-type"] = "text/html; charset=utf-8"
		r.set_data( html )

b = Browser()
b.addheaders.append( ['Accept-Encoding','gzip'] )
r ='')

About this entry

Good Reads