Reference

This is the API reference for the Pando library.

body_parsers

This module contains Pando’s built-in body parsers.

Body parsers are optional ways to enable Pando to uniformly parse POST body content according to its supplied Content-Type.

A body parser has the signature:

def name(raw, headers):

where raw is the raw bytestring to be parsed, and headers is the Headers mapping of the supplied headers.

pando.body_parsers.formdata(raw, headers)

Parse raw as form data.

Supports application/x-www-form-urlencoded and multipart/form-data.

pando.body_parsers.jsondata(raw, headers)

Parse raw as JSON data.

exceptions

Custom exceptions raised by Pando

exception pando.exceptions.CRLFInjection

A 400 Response (per #249) raised if there’s a suspected CRLF Injection attack in the headers.

exception pando.exceptions.MalformedBody(msg)

A 400 Response raised if parsing the body of a POST request fails.

exception pando.exceptions.UnknownBodyType(ctype)

A 415 Response raised if the Content-Type of the body of a POST request doesn’t have a body_parser registered for it.

exception pando.exceptions.BadLocation(msg)

A 500 Response raised if an invalid redirect is attempted.

http

baseheaders

class pando.http.baseheaders.BaseHeaders(headers=())

Bases: pando.http.mapping.BytesMapping, pando.http.mapping.CaseInsensitiveMapping

Represent the headers in an HTTP Request or Response message.

How to send non-English unicode string using HTTP header? and What character encoding should I use for a HTTP header? have good notes on why we do everything as pure bytes here.

__init__(headers=())

Takes headers as a dict, or list of items.

__setitem__(name, value)

Checks for CRLF in value, then calls the superclass method:

CaseInsensitiveMapping.__setitem__(name, value)
add(name, value)

Checks for CRLF in value, then calls the superclass method:

CaseInsensitiveMapping.add(name, value)
raw

Return the headers as a bytestring, formatted for an HTTP message.

__contains__(k) → True if D has a key k, else False
__getitem__(name)
__slots__ = ('encoding', 'encoding_errors')
all(name)
encoding
encoding_errors
get(name, default=None)
keyerror(name)

Raises a 400 Response.

ones(*names)

Given one or more names of keys, return a list of their values.

pop(name, default=<object object>)
popall(name, *default)

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised

mapping

class pando.http.mapping.Mapping(*a, **kw)

Bases: aspen.http.mapping.Mapping

__init__(*a, **kw)

Initializes the mapping.

Loops through positional arguments first, then through keyword args.

Positional arguments can be dicts or lists of items.

keyerror(name)

Raises a 400 Response.

__getitem__(name)

Given a name, return the last value or call self.keyerror.

__setitem__(name, value)

Given a name and value, clobber any existing values.

add(name, value)

Given a name and value, clobber any existing values with the new one.

all(name)

Given a name, return a list of values, possibly empty.

get(name, default=None)

Override to only return the last value.

ones(*names)

Given one or more names of keys, return a list of their values.

pop(name, default=<object object>)

Given a name, return a value.

This removes the last value from the list for name and returns it. If there was only one value in the list then the key is removed from the mapping. If name is not present and default is given, that is returned instead. Otherwise, self.keyerror is called.

popall()

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised

class pando.http.mapping.CaseInsensitiveMapping(*a, **kw)

Bases: pando.http.mapping.Mapping

__contains__(k) → True if D has a key k, else False
__getitem__(name)
__setitem__(name, value)
add(name, value)
get(name, default=None)
all(name)
pop(name, default=<object object>)
popall(name)

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised

__init__(*a, **kw)

Initializes the mapping.

Loops through positional arguments first, then through keyword args.

Positional arguments can be dicts or lists of items.

keyerror(name)

Raises a 400 Response.

ones(*names)

Given one or more names of keys, return a list of their values.

class pando.http.mapping.BytesMapping(*a, **kw)

Bases: pando.http.mapping.Mapping

This mapping automatically transcodes keys and values.

Attributes:
encoding (str): UTF-8 by default encoding_errors (str): ‘backslashreplace’ by default
>>> m = BytesMapping()
>>> m[b'foo'] = b'bar'
>>> m[b'foo']
b'bar'
>>> m['foo']
'bar'
__slots__ = ('encoding', 'encoding_errors')
encoding
encoding_errors
__contains__(k) → True if D has a key k, else False
__getitem__(name)
__setitem__(name, value)
add(name, value)
get(name, default=None)
all(name)
pop(name, default=<object object>)
popall(name, *default)

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised

keyerror(name)

Raises a 400 Response.

ones(*names)

Given one or more names of keys, return a list of their values.

response

class pando.http.response.CloseWrapper(request, body)

Conform to WSGI’s facility for running code after a response is sent.

__iter__()
close()
exception pando.http.response.Response(code=200, body='', headers=None)

Represent an HTTP Response message.

request = None
whence_raised = (None, None)
__init__(code=200, body='', headers=None)

Takes an int, a string, a dict.

  • code an HTTP response code, e.g., 404
  • body the message body as a string
  • headers a dict, list, or bytestring of HTTP headers

Code is first because when you’re raising your own Responses, they’re usually error conditions. Body is second because one more often wants to specify a body without headers, than a header without a body.

to_wsgi(environ, start_response, charset)
__repr__() <==> repr(x)
__str__() <==> str(x)
set_whence_raised()

Sets and returns the value of self.whence_raised.

It’s a tuple, (filename, linenum) where we were raised from.

This function needs to be called from inside the except block.

logging

Pando logging convenience wrappers

pando.logging.log(*messages, **kw)

Make logging more convenient - use magic to get the __name__ of the calling module/function and log as it.

‘level’ if present as a kwarg, is the level to log at. ‘upframes’ if present as a kwarg, is how many frames up to look for the name.

other kwargs are passed through to Logger.log()

pando.logging.log_dammit(*messages, **kw)

like log(), but critical instead of warning

testing