本文整理匯總了Python中requests.utils.get_encoding_from_headers方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.get_encoding_from_headers方法的具體用法?Python utils.get_encoding_from_headers怎麽用?Python utils.get_encoding_from_headers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests.utils
的用法示例。
在下文中一共展示了utils.get_encoding_from_headers方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build_response
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def build_response(self, req, resp):
response = CachedResponse()
response.status_code = getattr(resp, "status", None)
response.headers = CaseInsensitiveDict(getattr(resp, "headers", {}))
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response.reason = resp.reason
if isinstance(req.url, bytes):
response.url = req.url.decode("utf-8")
else:
response.url = req.url
extract_cookies_to_jar(response.cookies, req, resp)
response.request = req
response.connection = self
return response
示例2: make_response
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def make_response(status_code: int = 200,
content: bytes = b'',
headers: dict = None,
reason: str = None,
encoding: str = None,
) -> Response:
response = Response()
response.status_code = status_code
response._content = content
response._content_consumed = True
response.headers = CaseInsensitiveDict(headers or {})
response.encoding = encoding or get_encoding_from_headers(headers or {})
response.reason = reason
return response
示例3: build_response_into
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def build_response_into(self, response, req, urllib3_resp):
"""Same as requests.adapters.HTTPAdapter.build_response, but writes
into a provided requests.Response object instead of creating a new one.
"""
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(urllib3_resp, 'status', None)
# Make headers case-insensitive.
response.headers = CaseInsensitiveDict(
getattr(urllib3_resp, 'headers', {})
)
# Set encoding.
response.encoding = get_encoding_from_headers(response.headers)
response.raw = urllib3_resp
response.reason = response.raw.reason
if isinstance(req.url, bytes):
response.url = req.url.decode('utf-8')
else:
response.url = req.url
# Add new cookies from the server.
extract_cookies_to_jar(response.cookies, req, urllib3_resp)
# Give the Response some context.
response.request = req
response.connection = self
示例4: response
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def response(self, status_code=200, content='', headers=None,
reason=None, elapsed=0, request=None, stream=False):
res = requests.Response()
res.status_code = status_code
if isinstance(content, (dict, list)):
content = json.dumps(content).encode('utf-8')
if isinstance(content, str):
content = content.encode('utf-8')
res._content = content
res._content_consumed = content
res.headers = structures.CaseInsensitiveDict(headers or {})
res.encoding = utils.get_encoding_from_headers(res.headers)
res.reason = reason
res.request = request
if hasattr(request, 'url'):
res.url = request.url
if isinstance(request.url, bytes):
res.url = request.url.decode('utf-8')
if stream:
res.raw = BytesIO(content)
else:
res.raw = BytesIO(b'')
# normally this closes the underlying connection,
# but we have nothing to free.
res.close = lambda *args, **kwargs: None
return res
示例5: get_unicode_from_response
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def get_unicode_from_response(response):
"""Return the requested content back in unicode.
This will first attempt to retrieve the encoding from the response
headers. If that fails, it will use
:func:`requests_toolbelt.utils.deprecated.get_encodings_from_content`
to determine encodings from HTML elements.
.. code-block:: python
import requests
from requests_toolbelt.utils import deprecated
r = requests.get(url)
text = deprecated.get_unicode_from_response(r)
:param response: Response object to get unicode content from.
:type response: requests.models.Response
"""
tried_encodings = set()
# Try charset from content-type
encoding = utils.get_encoding_from_headers(response.headers)
if encoding:
try:
return str(response.content, encoding)
except UnicodeError:
tried_encodings.add(encoding.lower())
encodings = get_encodings_from_content(response.content)
for _encoding in encodings:
_encoding = _encoding.lower()
if _encoding in tried_encodings:
continue
try:
return str(response.content, _encoding)
except UnicodeError:
tried_encodings.add(_encoding)
# Fall back:
if encoding:
try:
return str(response.content, encoding, errors='replace')
except TypeError:
pass
return response.text
示例6: build_response
# 需要導入模塊: from requests import utils [as 別名]
# 或者: from requests.utils import get_encoding_from_headers [as 別名]
def build_response(req, delegate, cookiestore):
# type: (PreparedRequest, NSURLSessionAdapterDelegate, NSHTTPCookieStorage) -> Response
"""Build a requests `Response` object from the response data collected by the NSURLSessionDelegate, and the
default cookie store."""
response = Response()
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(delegate, 'status', None)
# Make headers case-insensitive.
response.headers = CaseInsensitiveDict(getattr(delegate, 'headers', {}))
# Set encoding.
response.encoding = get_encoding_from_headers(response.headers)
# response.raw = resp
# response.reason = response.raw.reason
if isinstance(req.url, bytes):
response.url = req.url.decode('utf-8')
else:
response.url = req.url
# Add new cookies from the server. For NSURLSession these have already been parsed.
# jar = RequestsCookieJar()
# for cookie in cookiestore.cookies():
# print cookie
# c = Cookie(
# version=cookie.version(),
# name=cookie.name(),
# value=cookie.value(),
# port=8444,
# # port=cookie.portList()[-1],
# port_specified=0,
# domain=cookie.domain(),
# domain_specified=cookie.domain(),
# domain_initial_dot=cookie.domain(),
# path=cookie.path(),
# path_specified=cookie.path(),
# secure=!cookie.HTTPOnly(),
# expires=cookie.expiresDate(),
# comment=cookie.comment(),
# comment_url=cookie.commentURL(),
# )
# jar.set_cookie(c)
#
# response.cookies = jar
response.raw = io.BytesIO(buffer(delegate.output))
# Give the Response some context.
response.request = req
# response.connection = self
return response