Response object. This object is used to control the response that will be sent to the HTTP client. A handler function will take the response object and fill in various parts of the response. For example, a plain text response with the body ‘Some example content’ could be produced as:
def handler(request, response): response.headers.set("Content-Type", "text/plain") response.content = "Some example content"
The response object also gives access to a ResponseWriter, which allows direct access to the response socket. For example, one could write a similar response but with more explicit control as follows:
import time def handler(request, response): response.add_required_headers = False # Don't implicitly add HTTP headers response.writer.write_status(200) response.writer.write_header("Content-Type", "text/plain") response.writer.write_header("Content-Length", len("Some example content")) response.writer.end_headers() response.writer.write("Some ") time.sleep(1) response.writer.write("example content")
Note that when writing the response directly like this it is always necessary to either set the Content-Length header or set response.close_connection = True. Without one of these, the client will not be able to determine where the response body ends and will continue to load indefinitely.
Object representing the response to a HTTP request
- handler – RequestHandler being used for this response
- request – Request that this is the response for
Request associated with this Response.
The encoding to use when converting unicode to strings for output.
Boolean indicating whether mandatory headers should be added to the response.
Boolean, default False, indicating whether the body content should be sent when the request method is HEAD.
Boolean indicating whether output should be flushed automatically or only when requested.
The ResponseWriter for this response
Status tuple (code, message). Can be set to an integer, in which case the message part is filled in automatically, or a tuple.
List of HTTP headers to send with the response. Each item in the list is a tuple of (name, value).
The body of the response. This can either be a string or a iterable of response parts. If it is an iterable, any item may be a string or a function of zero parameters which, when called, returns a string.
Delete a cookie on the client by setting it to the empty string and to expire in the past
Iterator returning chunks of response body content.
If any part of the content is a function, this will be called and the resulting value (if any) returned.
Parameters: read_file –
- boolean controlling the behaviour when content
is a file handle. When set to False the handle will be returned directly allowing the file to be passed to the output in small chunks. When set to True, the entire content of the file will be returned as a string facilitating non-streaming operations like template substitution.
Set a cookie to be sent with a Set-Cookie header in the response
- name – String name of the cookie
- value – String value of the cookie
- max_age – datetime.timedelta int representing the time (in seconds) until the cookie expires
- path – String path to which the cookie applies
- domain – String domain to which the cookie applies
- secure – Boolean indicating whether the cookie is marked as secure
- httponly – Boolean indicating whether the cookie is marked as HTTP Only
- comment – String comment
- expires – datetime.datetime or datetime.timedelta indicating a time or interval from now when the cookie expires
Set the response status headers and body to indicate an error
Remove a cookie from those that are being sent with the response
Write the whole response
Write out the response content
Write out the status line and headers for the response
Dictionary-like object holding the headers for the response
Add a new header with a given name, not overwriting any existing headers with the same name
- key – Name of the header to add
- value – Value to set for the header
get(key, default=<object object>)¶
Get the set values for a particular header.
Set a header to a specific value, overwriting any previous header with the same name
- key – Name of the header to set
- value – Value to set the header to
Object providing an API to write out a HTTP response.
- handler – The RequestHandler being used.
- response – The Response associated with this writer.
After each part of the response is written, the output is flushed unless response.explicit_flush is False, in which case the user must call .flush() explicitly.
Convert unicode to bytes according to response.encoding.
Finish writing headers and write the separator.
Unless add_required_headers on the response is False, this will also add HTTP-mandated headers that have not yet been supplied to the response headers
Flush the output. Returns False if the flush failed due to the socket being closed by the remote end.
Write directly to the response, converting unicode to bytes according to response.encoding. Does not flush.
Write the body of the response.
Write a file-like object directly to the response in chunks. Does not flush.
Write out a single header for the response.
- name – Name of the header field
- value – Value of the header field
Write out the status line of a response.
- code – The integer status code of the response.
- message – The message of the response. Defaults to the message commonly used with the status code.