Python requests.utils.get_encoding_from_headers() Examples

The following are 8 code examples of requests.utils.get_encoding_from_headers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests.utils , or try the search function .
Example #1
Source File: requests_cache.py    From foxford_courses with MIT License 6 votes vote down vote up
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 
Example #2
Source File: utils.py    From rets with MIT License 5 votes vote down vote up
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 
Example #3
Source File: core.py    From requests-racer with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #4
Source File: test_fpga_ext_arq.py    From cyborg with Apache License 2.0 5 votes vote down vote up
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 
Example #5
Source File: deprecated.py    From addon with GNU General Public License v3.0 4 votes vote down vote up
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 
Example #6
Source File: deprecated.py    From Requester with MIT License 4 votes vote down vote up
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 
Example #7
Source File: nsurlsession_adapter.py    From python-jss with GNU General Public License v3.0 4 votes vote down vote up
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 
Example #8
Source File: deprecated.py    From bazarr with GNU General Public License v3.0 4 votes vote down vote up
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