Skip to content
Myr

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

2 files+114 added−1 removed

modified:requests/models.py+48 added-1 removed
8 unmodified lines
9import urllib10import urllib211import socket12import zlib13import cgi1415from urllib2 import HTTPError16from urlparse import urlparse, urlunparse, urljoin17from datetime import datetime
382 unmodified lines
400 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 406407 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, params411412 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("'\"")416417 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)439440 # 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)449450 # Fall back:451 return unicode(content, encoding, errors="replace")452453 def raise_for_status(self):454 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""455 if self.error:456 raise self.error
172 unmodified lines
added:test_unicode.py+66 added
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34from __future__ import with_statement56import unittest7import cookielib89try:10 import omnijson as json11except ImportError:12 import json1314import requests15161718HTTPBIN_URL = 'http://httpbin.org/'19HTTPSBIN_URL = 'https://httpbin.ep.io/'2021# HTTPBIN_URL = 'http://staging.httpbin.org/'22# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'232425def httpbin(*suffix):26 """Returns url for HTTPBIN resource."""2728 return HTTPBIN_URL + '/'.join(suffix)293031def httpsbin(*suffix):32 """Returns url for HTTPSBIN resource."""3334 return HTTPSBIN_URL + '/'.join(suffix)353637SERVICES = (httpbin, httpsbin)38394041class RequestsTestSuite(unittest.TestCase):42 """Requests test cases."""434445 def setUp(self):46 pass474849 def tearDown(self):50 """Teardown."""51 pass525354 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)636465if __name__ == '__main__':66 unittest.main()