python - Get response encoding using httplib/http.client -
how can incoming response's encoding using httplib/http.client?
i can see part of content-type using getheaders(), guess bad practise parse since might in few different formats, , supposed use 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 best way incoming encoding?
not direct answer, maybe find useful. use requests library.
there reason people stopped building own http libraries. in fact, httplib says use urllib
which 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'
Comments
Post a Comment