Decode received bytes according to content type encoding

suggest change

The received bytes have to be decoded with the correct character encoding to be interpreted as text:

import urllib.request

response = urllib.request.urlopen("http://stackoverflow.com/")
data = response.read()

encoding = response.info().get_content_charset()
html = data.decode(encoding)

get_content_charset() returns None when the header has no charset, and decode(None) raises TypeError. A bare urlopen of this URL now fails with HTTP Error 403: Forbidden before that line.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents