WSGI Wrappers

This section introduces classes used by pulsar wsgi application to pass a request/response state during an HTTP request.

The WsgiRequest is a thin wrapper around a WSGI environ dictionary. It contains only the environ as its private data. The WsgiResponse, which is available in the WsgiRequest.response attribute, is an iterable over bytestring with several utility methods for manipulating headers and asynchronous content.

Environ Mixin

class pulsar.apps.wsgi.wrappers.EnvironMixin(environ, name=None)[source]

A wrapper around a WSGI environ.

Instances of this class have the environ attribute as their only private data. Every other attribute is stored in the environ itself at the pulsar.cache wsgi-extension key.

environ

WSGI environ dictionary

cache

An attribute dictionary of pulsar-specific data stored in the environ at the wsgi-extension key pulsar.cache

connection

The Connection handling the request

get(key, default=None)[source]

Shortcut to the environ get method.

Wsgi Request

class pulsar.apps.wsgi.wrappers.WsgiRequest(environ, app_handler=None, urlargs=None)[source]

An EnvironMixin for wsgi requests.

content_types

List of content types this client supports as a ContentAccept object.

Obtained form the Accept request header.

charsets

List of charsets this client supports as a CharsetAccept object.

Obtained form the Accept-Charset request header.

encodings

List of encodings this client supports as Accept object.

Obtained form the Accept-Charset request header. Encodings in a HTTP term are compression encodings such as gzip. For charsets have a look at charsets attribute.

languages

List of languages this client accepts as LanguageAccept object.

Obtained form the Accept-Language request header.

cookies

Container of request cookies

app_handler

The WSGI application handling this request.

The WSGI handler is responsible for setting this value in the same way as the Router does.

urlargs

Dictionary of url parameters obtained when matching a router with this request path.

cfg

The config container of the server

response

The WsgiResponse for this client request.

is_xhr

True if this is an AJAX request

is_secure

True if this request is via a TLS connection

path

Shortcut to the environ PATH_INFO value.

method

The request method (uppercase).

data_and_files(data=True, files=True, stream=None)[source]

Retrieve body data.

Returns a two-elements tuple of a MultiValueDict containing data from the request body, and data from uploaded files.

If the body data is not ready, return a Future which results in the tuple.

The result is cached.

body_data()[source]

A MultiValueDict containing data from the request body.

url_data

A (cached) dictionary containing data from the QUERY_STRING in environ.

html_document

Return a cached instance of HtmlDocument.

get_host(use_x_forwarded=True)[source]

Returns the HTTP host using the environment or request headers.

get_client_address(use_x_forwarded=True)[source]

Obtain the client IP address

full_path(*args, **query)[source]

Return a full path

absolute_uri(location=None, scheme=None)[source]

Builds an absolute URI from location and variables available in this request.

If no location is specified, the relative URI is built from full_path().

redirect(path, **kw)[source]

Redirect to a different path

set_response_content_type(response_content_types=None)[source]

Evaluate the content type for the response to a client request.

The method uses the response_content_types parameter of accepted content types and the content types accepted by the client request and figures out the best match.

Wsgi Response

class pulsar.apps.wsgi.wrappers.WsgiResponse(status=None, content=None, response_headers=None, content_type=None, encoding=None, environ=None, can_store_cookies=True)[source]

A WSGI response.

Instances are callable using the standard WSGI call and, importantly, iterable:

response = WsgiResponse(200)

A WsgiResponse is an iterable over bytes to send back to the requesting client.

status_code

Integer indicating the HTTP status, (i.e. 200)

response

String indicating the HTTP status (i.e. ‘OK’)

status

String indicating the HTTP status code and response (i.e. ‘200 OK’)

content_type

The content type of this response. Can be None.

headers

The Headers container for this response.

environ

The dictionary of WSGI environment if passed to the constructor.

cookies

A python SimpleCookie container of cookies included in the request as well as cookies set during the response.

is_streamed

Check if the response is streamed.

A streamed response is an iterable with no length information. In this case streamed means that there is no information about the number of iterations.

This is usually True if a generator is passed to the response object.

close()[source]

Close this response, required by WSGI

Sets a cookie.

expires can be a string in the correct format or a datetime.datetime object in UTC. If expires is a datetime object then max_age will be calculated.

get_headers()[source]

The list of headers for this response

Wsgi File Wrapper

class pulsar.apps.wsgi.wrappers.FileWrapper(file, block=None)[source]

WSGI File wrapper class.

Available directly from the wsgi.file_wrapper key in the WSGI environ dictionary. Alternatively one can use the file_response() high level function for serving local files.