Powered by Tachyonic Project Luxon Framework v0.0.0

Luxon Framework for rapid application development. (luxon)

Version

WSGI Response Class

class luxon.core.handlers.wsgi.response.Response(environ, start_response)[source]

Represents an HTTP response to a client request.

Parameters:
  • env (dict) – A WSGI environment dict passed in from the server. As per PEP-3333.
  • start_response (function) – callback function supplied by the server which takes the HTTP status and headers as arguments.
status

HTTP status code. (e.g. ‘200’) Default 200.

Type:int
etags

The luxon.utils.http.Etags obj associated with the Response.

Type:obj
cache_control

The value of cache-control in the Response header.

Type:str
content_type

Mime format of the content (e.g. application/xml; charset=utf-8).

Type:str
expire

The value of expire in the Response header.

Type:str
last_modified

The value of last-modified in the Response header.

Type:str
age

The value of age in the Response header.

Type:int
append_header(name, value)[source]

Set or append a header for this response.

If the header already exists, the new value will be appended to it, delimited by a comma. Some header specifications support this format, Set-Cookie being the notable exceptions.

Names and values must be convertable to ‘str’ or be str’. Strings must contain only US-ASCII characters.

Parameters:
  • name (str) – Header name (case-insensitive).
  • value (str) – Value for the header.
body(obj)[source]

Set Response Body.

Accepts following objects:
‘str’, and ‘bytes’, if str will be encoded to bytes. file, iter like objects must return bytes. OrderedDict, dict and list will be translated json and encoded to ‘UTF-8’
Parameters:obj (object) – Any valid object for response body.
content_length

Value of bytes in response.

Returns:total octects for response.
Return type:int
delete_header(name)[source]

Delete a header that was previously set for this response.

If the header was not previously set, nothing is done (no error is raised).

Names and values must be convertable to ‘str’ or be str’. Strings must contain only US-ASCII characters.

Parameters:name (str) – Header name (case-insensitive).
get_header(name)[source]

Retrieve the raw string value for the given header.

Names and values must be convertable to ‘str’ or be str’. Strings must contain only US-ASCII characters.

Parameters:name (str) – Header name (case-insensitive).
Returns:The header’s value if set, otherwise None.
Return type:str
read()[source]

Reads the entire Response body.

Set a response cookie.

This method can be called multiple times to add one or more cookies to the response.

Parameters:
  • name (str) – Cookie name
  • value (str) – Cookie value
Keyword Arguments:
 
  • expires (datetime) –

    Specifies when the cookie should expire. By default, cookies expire when the user agent exits.

    Refer to RFC 6265, Section 4.1.2.1

  • max_age (int) –

    Defines the lifetime of the cookie in seconds. By default, cookies expire when the user agent exits. If both ‘max_age’ and ‘expires’ are set, the latter is ignored by the user agent.

    Refer to RFC 6265, Section 4.1.2.2

  • domain (str) –

    Restricts the cookie to a specific domain and any subdomains of that domain. By default, the user agent will return the cookie only to the origin server. When overriding this default behavior, the specified domain must include the origin server. Otherwise, the user agent will reject the cookie.

    Refer to RFC 6265, Section 4.1.2.3

  • path (str) –

    Scopes the cookie to the given path plus any subdirectories under that path (the “/” character is interpreted as a directory separator). If the cookie does not specify a path, the user agent defaults to the path component of the requested URI.

    User agent interfaces do not always isolate cookies by path and so this should not be considered an effective security measure.

    Refer to RFC 6265, Section 4.1.2.4

  • secure (bool) –

    Direct the client to only return the cookie in subsequent requests if they are made over HTTPS

    Defaults to False.

    Enabling this can help prevents attackers from reading sensitive cookie data.

    Refer RFC 6265, Section 4.1.2.5

  • http_only (bool) –

    Direct the client to only transfer the cookie with unscripted HTTP requests.

    Default True.

    This is intended to mitigate some forms of cross-site scripting.

    Refer to RFC 6265, Section 4.1.2.6

Raises:
  • KeyErrorname is not a valid cookie name.
  • ValueErrorvalue is not a valid cookie value.
set_header(name, value)[source]

Set a header for this response to a given value.

Names and values must be convertable to ‘str’ or be str’. Strings must contain only US-ASCII characters.

Parameters:
  • name (str) – Header name (case-insensitive).
  • value (str) – Value for the header.
set_headers(headers)[source]

Set several headers at once.

Calling this method overwrites existing values, if any.

Names and values must be convertable to ‘str’ or be str’. Strings must contain only US-ASCII characters.

Parameters:headers (dict or list) – A dictionary of header names and values to set, or a ‘list’ of (name, value) tuples.
Raises:ValueErrorheaders was not a ‘dict’ or ‘list’ of ‘tuple’.

Unset a cookie in the response

Clears the contents of the cookie, and instructs the user agent to immediately expire its own copy of the cookie.

In order to successfully remove a cookie, both the path and the domain must match the values that were used when the cookie was created.

Parameters:name (str) – Cookie Name
write(value)[source]

Write bytes to response body.

Parameters:value (bytes) – Data to be written.
Returns:The number of bytes written.
Return type:int