gws.base.web.wsgi

WSGI requester and responder based on Werkzeug.

Source code: gws.base.web.wsgi

Module Contents

class gws.base.web.wsgi.Requester(root: gws.Root, environ: dict, site: gws.WebSite, **kwargs)

Bases: gws.WebRequester

Web requester that wraps a Werkzeug request.

contentType

Request content type.

contentTypeHeader
environ

Request environment.

host = ''

Request host name, without the port, empty if the host is invalid.

ip

Client address.

isApi = False

The request provides json data in the POST body.

isForm = False

The request provides form data in the POST body.

isGet

The request is a GET request.

isPost

The request is a POST request.

isSecure = False

The request is made over https.

maxContentLength
method

Request method.

port = 0

Request port, 0 if not given.

root

Object tree root.

scheme = 'http'

Request scheme, http or https.

session

Current session.

site

Website the request is processed for.

structInput = None
structOutput = None
user

Current user.

absolute_url_for(path, **params)

Create an absolute URL pointing to the requested host.

Parameters:
  • request_path – Request path.

  • **params – Additional GET parameters.

Returns:

A URL.

api_responder(response)

Create a responder for an API (structured) response.

The response is encoded as JSON or MessagePack, depending on the request.

Parameters:

response – Response object.

Returns:

A responder.

canonical_url_for(path, **params)

Create an absolute URL pointing to the canonical host.

Parameters:
  • request_path – Request path.

  • **params – Additional GET parameters.

Returns:

A URL.

command()

Return the command name.

Returns:

The command from the path, or from the cmd parameter.

content_responder(response)

Create a responder for a content response.

Parameters:

response – Response with literal content or a content path.

Returns:

A responder.

cookie(key, default='')

Get a cookie.

Parameters:
  • key – Cookie name.

  • default – Default value.

Returns:

A cookie value.

data()

Return the raw POST data, unzipped if gzip-encoded.

Returns:

POST data, empty for non-POST requests.

Raises:

gws.base.web.error.HTTPException – If the content length is invalid or too large, or the data cannot be unzipped.

env(key, default='')

Get a WSGI environment variable.

Parameters:
  • key – Variable name.

  • default – Default value.

Returns:

A variable value.

error_responder(exc)

Create a responder for an exception.

Parameters:

exc – An exception; anything but an HTTP exception results in a 500 response.

Returns:

A responder.

form()

Return the POST form data.

Returns:

A list of (key, value) tuples, including uploaded files; empty if the request is not a form.

Raises:

gws.base.web.error.HTTPException – If the form data cannot be decoded.

has_param(key)

Check if a GET parameter exists, ignoring case.

Parameters:

key – Parameter name.

Returns:

True if the parameter exists.

header(key, default='')

Get a header.

Parameters:
  • key – Header name.

  • default – Default value.

Returns:

A header value.

param(key, default='')

Get a GET parameter, ignoring case.

Parameters:
  • key – Parameter name.

  • default – Default value.

Returns:

A parameter value.

params()

Return the GET parameters.

Returns:

Parameters from the query string and from the request path.

parse()

Parse the request path, the parameters and the structured payload.

Also determines the scheme, host, port and client address, taking proxy headers into account if the site has proxies.

Raises:

gws.base.web.error.HTTPException – If the request path, the host or the payload is invalid.

path()

Return the request path after the command.

Returns:

The rest of the path after /_/<command>/.

query_params()

Return the GET parameters from the query string.

Returns:

Query string parameters.

redirect_responder(response)

Create a responder for a redirect response.

Parameters:

response – Redirect response.

Returns:

A responder.

relative_url_for(path, **params)

Create a host-relative URL.

Parameters:
  • request_path – Request path.

  • **params – Additional GET parameters.

Returns:

A URL.

set_session(sess)

Attach a session to the requester and set the user from it.

Parameters:

session – A session object.

struct()

Return the structured payload of an API request.

Returns:

The decoded JSON or MessagePack payload.

text()

Return the POST data decoded to text, with the charset of the request.

Returns:

POST data as text.

Raises:

gws.base.web.error.HTTPException – If the data cannot be decoded.

class gws.base.web.wsgi.Responder(**kwargs)

Bases: gws.WebResponder

Web responder that wraps a Werkzeug response.

status

Response status.

add_header(key, value)

Add a header.

Parameters:
  • key – Header name.

  • value – Header value.

Delete a cookie.

Parameters:
  • key – Cookie name.

  • **kwargs – Cookie options.

send_response(environ, start_response)

Send the response to the client.

Parameters:
  • environ – WSGI environment.

  • start_response – WSGI start_response function.

Returns:

The WSGI response iterable.

set_body(body)

Set the response body.

Parameters:

body – New response body; strings are encoded as UTF-8.

Set a cookie.

Parameters:
  • key – Cookie name.

  • value – Cookie value.

  • **kwargs – Cookie options.

set_status(status)

Set the response status.

Parameters:

status – HTTP status code.