Go to the latest version of this document.

# ``twisted`.`web`.`client`.`Response``

class documentation

`class Response: (source)`

Constructor: `Response(version, code, phrase, headers, _transport)`

Implements interfaces: `twisted.web.iweb.IResponse`

View In Hierarchy

A `Response` instance describes an HTTP response received from an HTTP server.

`Response` should not be subclassed or instantiated.

Method | `__init__` | No summary
Method | `deliverBody` | Dispatch the given `IProtocol` depending of the current state of the response.
Method | `setPreviousResponse` | Set the reference to the previous `IResponse`.
Instance Variable | `code` | The HTTP status code of this response, as a `int`.
Instance Variable | `headers` | The HTTP response `Headers` of this response.
Instance Variable | `phrase` | The HTTP reason phrase of this response, as a `str`.
Instance Variable | `previousResponse` | The previous `IResponse` from a redirect, or `None` if there was no previous response. This can be used to walk the response or request history for redirections.
Instance Variable | `request` | The `IClientRequest` that resulted in this response.
Instance Variable | `version` | A three-tuple describing the protocol and protocol version of the response. The first element is of type `str`, the second and third are of type `int`. For example, `(b'HTTP', 1, 1)`.
Class Method | `_construct` | Private constructor.
Method | `_bodyDataFinished` | Called by HTTPClientParser when no more body data is available. If the optional reason is supplied, this indicates a problem or potential problem receiving all of the response body.
Method | `_bodyDataFinished_CONNECTED` | Disconnect the protocol and move to the `'FINISHED'` state.
Method | `_bodyDataFinished_DEFERRED_CLOSE` | It is invalid to attempt to notify the `Response` of the end of the response body data more than once.
Method | `_bodyDataFinished_FINISHED` | It is invalid to attempt to notify the `Response` of the end of the response body data more than once.
Method | `_bodyDataFinished_INITIAL` | Move to the `'DEFERRED_CLOSE'` state to wait for a protocol to which to deliver the response body.
Method | `_bodyDataReceived` | Called by HTTPClientParser with chunks of data from the response body. They will be buffered or delivered to the protocol passed to deliverBody.
Method | `_bodyDataReceived_CONNECTED` | Deliver any data received to the protocol to which this `Response` is connected.
Method | `_bodyDataReceived_DEFERRED_CLOSE` | It is invalid for data to be delivered after it has been indicated that the response body has been completely delivered.
Method | `_bodyDataReceived_FINISHED` | It is invalid for data to be delivered after the response body has been delivered to a protocol.
Method | `_bodyDataReceived_INITIAL` | Buffer any data received for later delivery to a protocol passed to `deliverBody`.
Method | `_deliverBody_CONNECTED` | It is invalid to attempt to deliver data to a protocol when it is already being delivered to another protocol.
Method | `_deliverBody_DEFERRED_CLOSE` | Deliver any buffered data to `protocol` and then disconnect the protocol. Move to the `'FINISHED'` state.
Method | `_deliverBody_FINISHED` | It is invalid to attempt to deliver data to a protocol after the response body has been delivered to another protocol.
Method | `_deliverBody_INITIAL` | Deliver any buffered data to `protocol` and prepare to deliver any future data to it. Move to the `'CONNECTED'` state.
Class Variable | `_bodyFinished` | Undocumented
Instance Variable | `_bodyBuffer` | A `list` of the strings passed to `bodyDataReceived` before `deliverBody` is called. `None` afterwards.
Instance Variable | `_bodyProtocol` | The `IProtocol` provider to which the body is delivered. `None` before one has been registered with `deliverBody`.
Instance Variable | `_reason` | Undocumented
Instance Variable | `_state` | Indicates what state this `Response` instance is in, particularly with respect to delivering bytes from the response body to an application-supplied protocol object. This may be one of `'INITIAL'`, `'CONNECTED'`...
Instance Variable | `_transport` | See `__init__`.

def __init__(self, version, code, phrase, headers, _transport): (source)

Parameters
version | HTTP version components protocol, major, minor. E.g. `(b'HTTP', 1, 1)` to mean `b'HTTP/1.1'`.
code:`int` | HTTP status code.
phrase | HTTP reason phrase, intended to give a short description of the HTTP status code.
headers:`twisted.web.http_headers.Headers` | HTTP response headers.
_transport | The transport which is delivering this response.

def deliverBody(self, protocol): (source)
from `twisted.web.iweb.IResponse`

Dispatch the given `IProtocol` depending of the current state of the response.

def setPreviousResponse(self, previousResponse): (source)
from `twisted.web.iweb.IResponse`

Set the reference to the previous `IResponse`.

The value of the previous response can be read via `IResponse.previousResponse`.

code = (source)
from `twisted.web.iweb.IResponse`

The HTTP status code of this response, as a `int`.

headers = (source)
from `twisted.web.iweb.IResponse`

The HTTP response `Headers` of this response.

phrase = (source)
from `twisted.web.iweb.IResponse`

The HTTP reason phrase of this response, as a `str`.

previousResponse = (source)
from `twisted.web.iweb.IResponse`

The previous `IResponse` from a redirect, or `None` if there was no previous response. This can be used to walk the response or request history for redirections.

request = (source)
from `twisted.web.iweb.IResponse`

The `IClientRequest` that resulted in this response.

version = (source)
from `twisted.web.iweb.IResponse`

A three-tuple describing the protocol and protocol version of the response. The first element is of type `str`, the second and third are of type `int`. For example, `(b'HTTP', 1, 1)`.

@classmethod
def _construct(cls, version, code, phrase, headers, _transport, request): (source)

Private constructor.

Parameters
version | See `__init__`.
code | See `__init__`.
phrase | See `__init__`.
headers | See `__init__`.
_transport | See `__init__`.
request | See `IResponse.request`.

Returns
`Response` instance.

def _bodyDataFinished(self, reason=None): (source)

Called by HTTPClientParser when no more body data is available. If the optional reason is supplied, this indicates a problem or potential problem receiving all of the response body.

def _bodyDataFinished_CONNECTED(self, reason=None): (source)

Disconnect the protocol and move to the `'FINISHED'` state.

def _bodyDataFinished_DEFERRED_CLOSE(self): (source)

It is invalid to attempt to notify the `Response` of the end of the response body data more than once.

def _bodyDataFinished_FINISHED(self): (source)

It is invalid to attempt to notify the `Response` of the end of the response body data more than once.

def _bodyDataFinished_INITIAL(self, reason=None): (source)

Move to the `'DEFERRED_CLOSE'` state to wait for a protocol to which to deliver the response body.

def _bodyDataReceived(self, data): (source)

Called by HTTPClientParser with chunks of data from the response body. They will be buffered or delivered to the protocol passed to deliverBody.

def _bodyDataReceived_CONNECTED(self, data): (source)

Deliver any data received to the protocol to which this `Response` is connected.

def _bodyDataReceived_DEFERRED_CLOSE(self, data): (source)

It is invalid for data to be delivered after it has been indicated that the response body has been completely delivered.

def _bodyDataReceived_FINISHED(self, data): (source)

It is invalid for data to be delivered after the response body has been delivered to a protocol.

def _bodyDataReceived_INITIAL(self, data): (source)

Buffer any data received for later delivery to a protocol passed to `deliverBody`.

Little or no data should be buffered by this method, since the transport has been paused and will not be resumed until a protocol is supplied.

def _deliverBody_CONNECTED(self, protocol): (source)

It is invalid to attempt to deliver data to a protocol when it is already being delivered to another protocol.

def _deliverBody_DEFERRED_CLOSE(self, protocol): (source)

Deliver any buffered data to `protocol` and then disconnect the protocol. Move to the `'FINISHED'` state.

def _deliverBody_FINISHED(self, protocol): (source)

It is invalid to attempt to deliver data to a protocol after the response body has been delivered to another protocol.

def _deliverBody_INITIAL(self, protocol): (source)

Deliver any buffered data to `protocol` and prepare to deliver any future data to it. Move to the `'CONNECTED'` state.

_bodyFinished: `bool` = (source)

Undocumented

_bodyBuffer = (source)

A `list` of the strings passed to `bodyDataReceived` before `deliverBody` is called. `None` afterwards.

_bodyProtocol = (source)

The `IProtocol` provider to which the body is delivered. `None` before one has been registered with `deliverBody`.

_reason = (source)

Undocumented

_state: `str` = (source)

Indicates what state this `Response` instance is in, particularly with respect to delivering bytes from the response body to an application-supplied protocol object. This may be one of `'INITIAL'`, `'CONNECTED'`, `'DEFERRED_CLOSE'`, or `'FINISHED'`, with the following meanings:

- INITIAL: This is the state `Response` objects start in. No protocol has yet been provided and the underlying transport may still have bytes to deliver to it.
- DEFERRED_CLOSE: If the underlying transport indicates all bytes have been delivered but no application-provided protocol is yet available, the `Response` moves to this state. Data is buffered and waiting for a protocol to be delivered to.
- CONNECTED: If a protocol is provided when the state is INITIAL, the `Response` moves to this state. Any buffered data is delivered and any data which arrives from the transport subsequently is given directly to the protocol.
- FINISHED: If a protocol is provided in the DEFERRED_CLOSE state, the `Response` moves to this state after delivering all buffered data to the protocol. Otherwise, if the `Response` is in the CONNECTED state, if the transport indicates there is no more data, the `Response` moves to this state. Nothing else can happen once the `Response` is in this state.

_transport = (source)

See `__init__`.