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_header("Content-Type", "text/plain")
    response.writer.write_header("Content-Length", len("Some example content"))
    response.writer.write("Some ")
    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.


class wptserve.response.H2Response(handler, request)

Write out the response content


Write out the status line and headers for the response

class wptserve.response.Response(handler, request, response_writer_cls=None)

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.


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 (code, message) in which case code is an int and message is a text or binary string.


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.


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 – name of the cookie (a binary string)

  • value – value of the cookie (a binary string, or None)

  • 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

  • samesit – String indicating whether the cookie should be restricted to same site context

  • 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_error(code, err=None)

Set the response status headers and return a JSON error object:

{“error”: {“code”: code, “message”: message}} code is an int (HTTP status code), and message is a text string.

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

class wptserve.response.ResponseHeaders

Dictionary-like object holding the headers for the response

append(key, value)

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(key, value)

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

class wptserve.response.ResponseWriter(handler, response)

Object providing an API to write out a HTTP response.

  • handler – The RequestHandler being used.

  • response – The Response associated with this writer.


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. :return: A boolean indicating whether the write succeeds


Write directly to the response, converting unicode to bytes according to response.encoding. :return: A boolean indicating whether the write succeeds


Write the body of the response.

HTTP-mandated headers will be automatically added with status default to 200 if they have not been explicitly set. :return: A boolean indicating whether the write succeeds


Write a file-like object directly to the response in chunks.

write_header(name, value)

Write out a single header for the response.

If a status has not been written, a default status will be written (currently 200)

  • name – Name of the header field

  • value – Value of the header field


A boolean indicating whether the write succeeds


Writes the data ‘as is’

write_status(code, message=None)

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.