2

How can I get the incoming response's encoding using httplib/http.client?

I can see as part of Content-Type using getheaders(), but I would guess it is bad practise to parse that since it might be in a few different formats, and you are supposed to use a particular method in httplib/http.client instead:

>>> r = h.getresponse()
>>> r.getheaders()
[('transfer-encoding', 'chunked'), ('expires', 'Tue, 11 Oct 1988 22:00:00 GMT'), ('vary', 'Accept-Encoding'), ('server', 'nginx/1.2.6'), ('connection', 'keep-alive'), ('pragma', 'no-cache'), ('cache-control', 'no-cache, must-revalidate'), ('date', 'Thu, 18 Apr 2013 00:46:18 GMT'), ('content-type', 'text/html; charset=utf-8')]

What is the best way to get the incoming encoding?

txgairhq
  • 21
  • 1

1 Answers1

0

Not a direct answer, but maybe you will find this useful. Use requests library.

There is a reason people stopped building their own http libraries. In fact, httplib even says use urllibwhich uses http library. In turns, requests uses urllib3.

>>> import requests
>>> r = requests.get("http://bitbucket.org")
dir>>> dir(r)
['__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'encoding', 'headers', 'history', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
>>> r.encoding
'utf-8'
CppLearner
  • 16,273
  • 32
  • 108
  • 163