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.Response(handler, request)[source]

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.

explicit_flush

Boolean indicating whether output should be flushed automatically or only when requested.

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.

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)[source]

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 – 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_error(code, message='')[source]

Set the response status headers and body to indicate an error

Remove a cookie from those that are being sent with the response

write()[source]

Write the whole response

write_content()[source]

Write out the response content

write_status_headers()[source]

Write out the status line and headers for the response

class wptserve.response.ResponseHeaders[source]

Dictionary-like object holding the headers for the response

append(key, value)[source]

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>)[source]

Get the set values for a particular header.

set(key, value)[source]

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)[source]

Object providing an API to write out a HTTP response.

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

encode(data)[source]

Convert unicode to bytes according to response.encoding.

end_headers()[source]

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()[source]

Flush the output.

write(data)[source]

Write directly to the response, converting unicode to bytes according to response.encoding. Does not flush.

write_content(data)[source]

Write the body of the response.

write_content_file(data)[source]

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

write_header(name, value)[source]

Write out a single header for the response.

Parameters:
  • name – Name of the header field
  • value – Value of the header field
write_status(code, message=None)[source]

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.