Response

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.

Interface

class wptserve.response.H2Response(handler, request)
write_content()

Write out the response content

write_status_headers()

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

Parameters:
  • handler – RequestHandler being used for this response

  • request – Request that this is the response for

request

Request associated with this Response.

encoding

The encoding to use when converting unicode to strings for output.

add_required_headers

Boolean indicating whether mandatory headers should be added to the response.

send_body_for_head_request

Boolean, default False, indicating whether the body content should be sent when the request method is HEAD.

writer

The ResponseWriter for this response

status

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.

headers

List of HTTP headers to send with the response. Each item in the list is a tuple of (name, value).

content

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

iter_content(read_file=False)

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

Parameters:
  • 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()

Write the whole response

write_content()

Write out the response content

write_status_headers()

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

Parameters:
  • 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

Parameters:
  • 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.

Parameters:
  • handler – The RequestHandler being used.

  • response – The Response associated with this writer.

encode(data)

Convert unicode to bytes according to response.encoding.

end_headers()

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(data)

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

write_content(data)

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_content_file(data)

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)

Parameters:
  • name – Name of the header field

  • value – Value of the header field

Returns:

A boolean indicating whether the write succeeds

write_raw_content(data)

Writes the data ‘as is’

write_status(code, message=None)

Write out the status line of a response.

Parameters:
  • code – The integer status code of the response.

  • message – The message of the response. Defaults to the message commonly used with the status code.