class documentation

class HTTPResponse(io.IOBase): (source)

View In Hierarchy

HTTP Response container. Backwards-compatible with :class:`http.client.HTTPResponse` but the response ``body`` is loaded and decoded on-demand when the ``data`` property is accessed. This class is also compatible with the Python standard library's :mod:`io` module, and can hence be treated as a readable object in the context of that framework. Extra parameters for behaviour not present in :class:`http.client.HTTPResponse`: :param preload_content: If True, the response's body will be preloaded during construction. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param original_response: When this HTTPResponse wrapper is generated from an :class:`http.client.HTTPResponse` object, it's convenient to include the original for debug purposes. It's otherwise unused. :param retries: The retries contains the last :class:`~urllib3.util.retry.Retry` that was used during the request. :param enforce_content_length: Enforce content length checking. Body returned by server must match value of Content-Length header, if present. Otherwise, raise error.

Class Method from_httplib Given an :class:`http.client.HTTPResponse` instance ``r``, return a corresponding :class:`urllib3.response.HTTPResponse` object.
Method __init__ Undocumented
Method __iter__ Undocumented
Method close Undocumented
Method drain_conn Read and discard any remaining HTTP response data in the response connection.
Method fileno Undocumented
Method flush Undocumented
Method get_redirect_location Should we redirect and where to?
Method getheader Undocumented
Method getheaders Undocumented
Method geturl Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location.
Method info Undocumented
Method isclosed Undocumented
Method read Similar to :meth:`http.client.HTTPResponse.read`, but with two additional parameters: ``decode_content`` and ``cache_content``.
Method read_chunked Similar to :meth:`HTTPResponse.read`, but with an additional parameter: ``decode_content``.
Method readable Undocumented
Method readinto Undocumented
Method release_conn Undocumented
Method stream A generator wrapper for the read() method. A call will block until ``amt`` bytes have been read from the connection or until the connection is closed.
Method supports_chunked_reads Checks if the underlying file-like object looks like a :class:`http.client.HTTPResponse` object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked().
Method tell Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:``urllib3.response.HTTPResponse.read`` if bytes are encoded on the wire (e.g, compressed).
Constant CONTENT_DECODERS Undocumented
Constant DECODER_ERROR_CLASSES Undocumented
Constant REDIRECT_STATUSES Undocumented
Instance Variable auto_close Undocumented
Instance Variable chunk_left Undocumented
Instance Variable chunked Undocumented
Instance Variable decode_content Undocumented
Instance Variable enforce_content_length Undocumented
Instance Variable headers Undocumented
Instance Variable length_remaining Undocumented
Instance Variable msg Undocumented
Instance Variable reason Undocumented
Instance Variable retries Undocumented
Instance Variable status Undocumented
Instance Variable strict Undocumented
Instance Variable version Undocumented
Property closed Undocumented
Property connection Undocumented
Property data Undocumented
Method _decode Decode the data passed in and potentially flush the decoder.
Method _error_catcher Catch low-level python exceptions, instead re-raising urllib3 variants, so that low-level exceptions are not leaked in the high-level api.
Method _flush_decoder Flushes the decoder. Should only be called if the decoder is actually being used.
Method _fp_read Read a response with the thought that reading the number of bytes larger than can fit in a 32-bit int at a time via SSL in some known cases leads to an overflow error that has to be prevented if `amt` or `self...
Method _handle_chunk Undocumented
Method _init_decoder Set-up the _decoder attribute if necessary.
Method _init_length Set initial length value for Response content if available.
Method _update_chunk_length Undocumented
Instance Variable _body Undocumented
Instance Variable _connection Undocumented
Instance Variable _decoder Undocumented
Instance Variable _fp Undocumented
Instance Variable _fp_bytes_read Undocumented
Instance Variable _original_response Undocumented
Instance Variable _pool Undocumented
Instance Variable _request_url Undocumented
@classmethod
def from_httplib(ResponseCls, r, **response_kw): (source)

Given an :class:`http.client.HTTPResponse` instance ``r``, return a corresponding :class:`urllib3.response.HTTPResponse` object. Remaining parameters are passed to the HTTPResponse constructor, along with ``original_response=r``.

def __init__(self, body='', headers=None, status=0, version=0, reason=None, strict=0, preload_content=True, decode_content=True, original_response=None, pool=None, connection=None, msg=None, retries=None, enforce_content_length=False, request_method=None, request_url=None, auto_close=True): (source)

Undocumented

def __iter__(self): (source)

Undocumented

def close(self): (source)

Undocumented

def drain_conn(self): (source)

Read and discard any remaining HTTP response data in the response connection. Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.

def fileno(self): (source)

Undocumented

def flush(self): (source)

Undocumented

def get_redirect_location(self): (source)

Should we redirect and where to? :returns: Truthy redirect location string if we got a redirect status code and valid location. ``None`` if redirect status and no location. ``False`` if not a redirect status code.

def getheader(self, name, default=None): (source)

Undocumented

def getheaders(self): (source)

Undocumented

def geturl(self): (source)

Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location.

def info(self): (source)

Undocumented

def isclosed(self): (source)

Undocumented

def read(self, amt=None, decode_content=None, cache_content=False): (source)

Similar to :meth:`http.client.HTTPResponse.read`, but with two additional parameters: ``decode_content`` and ``cache_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache partial content as the full response. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param cache_content: If True, will save the returned data such that the same result is returned despite of the state of the underlying file object. This is useful if you want the ``.data`` property to continue working after having ``.read()`` the file object. (Overridden if ``amt`` is set.)

def read_chunked(self, amt=None, decode_content=None): (source)

Similar to :meth:`HTTPResponse.read`, but with an additional parameter: ``decode_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache partial content as the full response. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header.

def readable(self): (source)

Undocumented

def readinto(self, b): (source)

Undocumented

def release_conn(self): (source)

Undocumented

def stream(self, amt=2**16, decode_content=None): (source)

A generator wrapper for the read() method. A call will block until ``amt`` bytes have been read from the connection or until the connection is closed. :param amt: How much of the content to read. The generator will return up to much data per iteration, but may return less. This is particularly likely when using compressed data. However, the empty string will never be returned. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header.

def supports_chunked_reads(self): (source)

Checks if the underlying file-like object looks like a :class:`http.client.HTTPResponse` object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked().

def tell(self): (source)

Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:``urllib3.response.HTTPResponse.read`` if bytes are encoded on the wire (e.g, compressed).

CONTENT_DECODERS: list[str] = (source)

Undocumented

Value
['gzip', 'deflate']
DECODER_ERROR_CLASSES = (source)

Undocumented

Value
(IOError, zlib.error)
REDIRECT_STATUSES: list[int] = (source)

Undocumented

Value
[301, 302, 303, 307, 308]
auto_close = (source)

Undocumented

chunk_left = (source)

Undocumented

Undocumented

decode_content = (source)

Undocumented

enforce_content_length = (source)

Undocumented

Undocumented

length_remaining = (source)

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

def _decode(self, data, decode_content, flush_decoder): (source)

Decode the data passed in and potentially flush the decoder.

@contextmanager
def _error_catcher(self): (source)

Catch low-level python exceptions, instead re-raising urllib3 variants, so that low-level exceptions are not leaked in the high-level api. On exit, release the connection back to the pool.

def _flush_decoder(self): (source)

Flushes the decoder. Should only be called if the decoder is actually being used.

def _fp_read(self, amt): (source)

Read a response with the thought that reading the number of bytes larger than can fit in a 32-bit int at a time via SSL in some known cases leads to an overflow error that has to be prevented if `amt` or `self.length_remaining` indicate that a problem may happen. The known cases: * 3.8 <= CPython < 3.9.7 because of a bug https://github.com/urllib3/urllib3/issues/2513#issuecomment-1152559900. * urllib3 injected with pyOpenSSL-backed SSL-support. * CPython < 3.10 only when `amt` does not fit 32-bit int.

def _handle_chunk(self, amt): (source)

Undocumented

def _init_decoder(self): (source)

Set-up the _decoder attribute if necessary.

def _init_length(self, request_method): (source)

Set initial length value for Response content if available.

def _update_chunk_length(self): (source)

Undocumented

Undocumented

_connection = (source)

Undocumented

_decoder = (source)

Undocumented

Undocumented

_fp_bytes_read: int = (source)

Undocumented

_original_response = (source)

Undocumented

Undocumented

_request_url = (source)

Undocumented