Return the r.content as unicode.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rick Mak · committed by Rick Mak · parent 8f72c48 · Browse files · 2 files·+114 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+114 added−1 removed
- other:+114 added−1 removed
Provenance
- Commit
8c73706- Tree
af9129a- Author
Rick Mak <rick.mak@gmail.com>- Committer
Rick Mak <rick.mak@gmail.com>- Parent
8f72c48
2 files changed+114 added−1 removed
modified:requests/models.py+48 added-1 removed
8 unmodified lines9 import urllib10 import urllib211 import socket12 import zlib13+import cgi14 15 from urllib2 import HTTPError16 from urlparse import urlparse, urlunparse, urljoin17 from datetime import datetime382 unmodified lines400 try:401 self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)402 except zlib.error:403 pass403- return self._content404+ return self.unicode_content(self._content)405+ 406 407+ def get_content_type(self):408+ content_type = self.headers.get("content-type")409+ content_type, params = cgi.parse_header(content_type)410+ return content_type, params411 412+ def get_encoding_from_content_type(self):413+ content_type, params = self.get_content_type()414+ if "charset" in params:415+ return params["charset"].strip("'\"")416+417+ def get_encodings_from_content(self, content):418+ if self._charset_re is None:419+ self._charset_re = re.compile(420+ r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I421+ )422+ return self._charset_re.findall(content)423+ 424+ def unicode_content(self, content):425+ """426+ Returns the requested content back in unicode.427+ Tried:428+ 1. charset from content-type429+ 2. every encodings from <meta ... charset=XXX>430+ 3. fall back and replace all unicode characters431+ """432+ # Try charset from content-type433+ encoding = self.get_encoding_from_content_type()434+ if encoding:435+ try:436+ return unicode(content, encoding)437+ except UnicodeError:438+ self.tried_encodings.append(encoding)439+440+ # Try every encodings from <meta ... charset=XXX>441+ encodings = self.get_encodings_from_content(content)442+ for encoding in encodings:443+ if encoding in self.tried_encodings:444+ continue445+ try:446+ return unicode(content, encoding)447+ except UnicodeError:448+ self.tried_encodings.append(encoding)449+450+ # Fall back:451+ return unicode(content, encoding, errors="replace")452+453 def raise_for_status(self):454 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""455 if self.error:456 raise self.error172 unmodified linesadded:test_unicode.py+66 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+4+from __future__ import with_statement5+6+import unittest7+import cookielib8+9+try:10+ import omnijson as json11+except ImportError:12+ import json13+14+import requests15+16+17+18+HTTPBIN_URL = 'http://httpbin.org/'19+HTTPSBIN_URL = 'https://httpbin.ep.io/'20+21+# HTTPBIN_URL = 'http://staging.httpbin.org/'22+# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'23+24+25+def httpbin(*suffix):26+ """Returns url for HTTPBIN resource."""27+28+ return HTTPBIN_URL + '/'.join(suffix)29+30+31+def httpsbin(*suffix):32+ """Returns url for HTTPSBIN resource."""33+34+ return HTTPSBIN_URL + '/'.join(suffix)35+36+37+SERVICES = (httpbin, httpsbin)38+39+40+41+class RequestsTestSuite(unittest.TestCase):42+ """Requests test cases."""43+44+45+ def setUp(self):46+ pass47+48+49+ def tearDown(self):50+ """Teardown."""51+ pass52+53+54+ def test_HTTP_200_OK_GET_ON_ISO88591(self):55+ r = requests.get("http://www.qypedeals.de/Verzehrgutschein+für+Jellyfish")56+ self.assertEqual(r.status_code, 200)57+ self.assertIsInstance(r.content, unicode)58+ 59+ def test_HTTP_200_OK_GET_ON_BIG5(self):60+ r = requests.get("http://google.com.hk/")61+ self.assertEqual(r.status_code, 200)62+ self.assertIsInstance(r.content, unicode)63+64+65+if __name__ == '__main__':66+ unittest.main()