Pycopia WWW package

Modules for working with the world wide web protocols and documents. This package contains the web application framework ad various document parsers and generators.

The primary idea behind this framework is to enable content generation in pure Python. There are no HTML templates or embedded pseudo-HTML strings in the applications source code. Instead, you construct pages using the API found in the pycopia.WWW.XHTML module. This pattern is more like GUI construction using something like Tkinter module. It’s primarily intended for highly dynamic “web” interfaces of dedicated servers or network “appliances”.

For database access the framework does not impose ANY special requirements, nor provide its own ORM. You use what you wish, but would usually be the best-in-class Sqlalchemy. But you could use the DBAPI directly if you wish.

The primary use-case that this web framework is targeted for is small-scale web front-end for administrative webUI of network appliances or services. Primarily for intra-net use (private networks).

The framework is implemented as a collection of FCGI servers front-ended by the lighttpd web server. Each FCGI server has its own configuration and may be run under a different user account (non-root). This provides some additional security against server vulnerabilities by limiting the resources available to it to only what it needs. The underlying user-based OS security model is used to enforce this.

Each FCGI server is seamlessly mapped into a URL scheme. This scheme is dynamic. The URL path directs a request to a specific server. When a new FCGI server is added it is accessed by using its name as the first element in the URL’s path.

For example, you configure a server with the name myservice. It would then be available under http://myhost.mydomain/myservice/. Further path elements are directed to specific handers in the server implementation.

The rest of the URL scheme is determined by regular expressions in a configuration file. Each handler is a bit of Python code.

The framework is also multi-user, and enables tasks to be run on the host with the credentials of the authenticated user.

Documents

These module provide the API for dynamic construction of XHTML or HTML5 (XML serialization) documents in pure Python. They also provide parsers to parse XHTML text into an object tree where you can modify it and then re-serialize it to text.

This package implements the XHTML specification using the XML.POM (Pythonic Object Model).

the XHTMLDocument class can be used to construct new XHTML documents. There are many helper methods to construct a document from dtd objects.

You can use this as a pure-Python method of markup generation. No templates are required. It can be used with the Pycopia web server framework, or other frameworks such as Django.

The general pattern of constructor methods is this. If a method starts with get_ then it is a factory method that construct an elemement node that is not attached to the document tree. Methods starting with add_ call the same factory method but also adds (appends) the new object to the document tree at the node where it was called. Methods starting with new_ add new element to the document tree that may be a more complex operation, or take additional parameters or build more complex subtrees. They also return the new object.

When complete, the tree can be serialized by stringifying it, or by using the emit method.

class pycopia.WWW.XHTML.ContainerMixin[source]
add_anchor(**attribs)[source]
add_break(**kwargs)[source]
add_comment(text)[source]
add_definition_list(**attribs)[source]
add_form(**kwargs)[source]
add_header(level, text, **kwargs)[source]
add_inline(_name, **kwargs)[source]
add_ordered_list(items, **kwargs)[source]
add_para(**attribs)[source]
add_preformat(**kwargs)[source]
add_ruler(**kwargs)[source]
add_section(_name, **kwargs)[source]
add_table(**kwargs)[source]
add_unordered_list(items, **kwargs)[source]
get_anchor(**attribs)[source]
get_break(**kwargs)[source]
get_definition_list(**attribs)[source]
get_form(**kwargs)[source]
get_header(level, text, **kwargs)[source]
get_inline(_name, **kwargs)[source]
get_ordered_list(**attribs)[source]
get_para(**attribs)[source]
get_preformat(**kwargs)[source]
get_ruler(**kwargs)[source]
get_section(_name, **kwargs)[source]
get_table(**kwargs)[source]
get_unordered_list(**attribs)[source]
new_anchor(obj, **attribs)[source]
new_inline(_name, _obj, **kwargs)[source]
new_para(data, **attribs)[source]
new_preformat(text, **kwargs)[source]
new_section(_name, _data, **kwargs)[source]
new_table(rowiter, coliter, headings=(), renderer=<function _NULLRenderer at 0x58a2c08>, **kwargs)[source]

Construct a new table. Row iterator adds rows with first column filled in with object, formatted by calling the renderer callback with the object (which should return something). Column iterator fills in extra columns by using a callback with a nodemaker and row object as parameters. Supply a tuple of values for the “headings” parameter to set the table headings. Other keyword arguments are passed on as table attributes.

new_table_from_GenericTable(table, renderer=<function _NULLRenderer at 0x58a2c08>, **kwargs)[source]

Construct an XHTML table from a pycopia.table.GenericTable object.

class pycopia.WWW.XHTML.DefinitionListMixin[source]
add_definition(term, data, **kwargs)[source]
add_definitions(defmap, **kwargs)[source]

Add a set of definitions from a dictionary, where the keys are the terms and the values are the data.

class pycopia.WWW.XHTML.DynamicNode(_method, *args, **kwargs)[source]

A psuedo-node that emits markup that is dynamic. This really wraps a markup generator as a node object, so that it may be emitted later with dynamic data.

destroy()[source]
detach()[source]
emit(fo, encoding)[source]
encode(encoding)[source]
fullpath
has_attributes()[source]
has_children()[source]
insert(data, encoding=None)[source]
matchpath(pe)[source]
walk(visitor)[source]
class pycopia.WWW.XHTML.FlowCreator(dtd)[source]
class pycopia.WWW.XHTML.FlowMixin[source]
add2class(name)[source]
add_element(name, **kwargs)[source]
add_image(**kwargs)[source]
add_javascript(text=None, src=None)[source]
add_object(_params=None, **kwargs)[source]
connect(signame, dest, func=None)[source]

http://mochikit.com/doc/html/MochiKit/Signal.html#fn-connect

create_markup(data)[source]

Create markup (a POM object tree) from Python objects.

creator
disconnect(*signals)[source]
get(name, **kwargs)
get_creator()[source]
get_dtd_element(name)[source]
get_image(**kwargs)[source]
get_javascript(text=None, src=None)[source]
get_new_element(name, **kwargs)[source]
get_nodemaker()[source]
get_object(_params=None, **kwargs)[source]
make_node(name, attribs, *content)[source]
new_image(_imagefile, _alt=None, **kwargs)[source]
nodemaker
class pycopia.WWW.XHTML.FormMixin[source]
add_checkboxes(name, choices, vertical=False, **kwargs)[source]
add_fieldset(legend=None, **kwargs)[source]
add_fileinput(name=u'fileinput', default=None)[source]
add_hidden(name, value)[source]
add_input(**kwargs)[source]
add_label(text, _for=None)[source]
add_password(name, label=None, default=u'', maxlength=255)[source]
add_radiobuttons(name, choices, vertical=False, checked=0, **kwargs)[source]
add_select(enums, **kwargs)[source]
add_textarea(name, text=None, rows=4, cols=60, **kwargs)[source]
add_textinput(name, label=None, default=u'', maxlength=255, **kwargs)[source]
choose(somelist, defidx=0, prompt=u'choose')[source]
fetch_form_elements()[source]

Return active form element nodes, grouped by name in a dictionary.

fetch_form_values(container=None)[source]
get_checkboxes(name, choices, vertical=False, **kwargs)[source]
get_fieldset(legend=None, **kwargs)[source]
get_input(**kwargs)[source]
get_label(text, _for=None)[source]
get_password(name, label=None, default=u'', maxlength=255)[source]
get_radiobuttons(name, choices, vertical=False, checked=0, **kwargs)[source]
get_select(enums, **kwargs)[source]
get_textarea(name, text=None, rows=4, cols=60, **kwargs)[source]
get_textinput(name, label=None, default=u'', maxlength=255, **kwargs)[source]
yes_no(prompt, name=u'yes_no', default=True)[source]
class pycopia.WWW.XHTML.GenericDocument(dtd=None, doctype=None, lang=None, encoding='utf-8')[source]

Generic markup document to be used as a default.

MIMETYPE = u'text/xml'
class pycopia.WWW.XHTML.InlineMixin[source]
Q(obj, **attribs)
abbr(obj, **attribs)[source]
acronym(obj, **attribs)[source]
add_inline(_name, **kwargs)[source]
anchor(obj, **attribs)[source]
big(obj, **attribs)[source]
bold(obj, **attribs)[source]
cite(obj, **attribs)[source]
code(obj, **attribs)[source]
dfn(obj, **attribs)[source]
em(obj, **attribs)[source]
get_inline(_name, **kwargs)[source]
italic(obj, **attribs)[source]
kbd(obj, **attribs)[source]
nbsp()[source]
new_inline(_name, _obj, **attribs)[source]
quote(obj, **attribs)[source]
samp(obj, **attribs)[source]
small(obj, **attribs)[source]
span(obj, **attribs)[source]
strong(obj, **attribs)[source]
sub(obj, **attribs)[source]
sup(obj, **attribs)[source]
teletype(obj, **attribs)[source]
text(text)[source]
var(obj, **attribs)[source]
class pycopia.WWW.XHTML.InputMixin[source]

type = (text | password | checkbox | radio | submit | reset | file | hidden | image | button)

set_autofocus(val=True)[source]
set_checked(val=True)[source]
set_disabled(val=True)[source]
set_readonly(val=True)[source]
class pycopia.WWW.XHTML.ListMixin[source]
add_item(obj, **attribs)[source]
class pycopia.WWW.XHTML.ObjectMixin[source]
add_param(name, value=None, valuetype=u'data', type=None, id=None)[source]
class pycopia.WWW.XHTML.PasswordWidget[source]
class pycopia.WWW.XHTML.RowMixin[source]
add_column(**kwargs)[source]
get_column(**kwargs)[source]
new_column(*args, **kwargs)[source]
class pycopia.WWW.XHTML.StringWidget[source]
class pycopia.WWW.XHTML.TableMixin[source]
add_row(**kwargs)[source]
caption(content, **kwargs)[source]
delete(col, row)[source]
emit(fo, encoding=None)[source]
encode(encoding)[source]
footer[source]
get_cell(col, row)[source]
get_row(**kwargs)[source]
headings[source]
new_headings(*args, **kwargs)[source]
new_row(*args, **kwargs)[source]
set_cell(col, row, val)[source]
set_heading(col, val, **kwargs)[source]

Set heading at column <col> (origin 1) to <val>.

class pycopia.WWW.XHTML.TextareaMixin[source]
set_disabled(val=True)[source]
set_readonly(val=True)[source]
class pycopia.WWW.XHTML.URL(base)[source]
class pycopia.WWW.XHTML.WidgetBase[source]
class pycopia.WWW.XHTML.XHTMLComment(data=u'', encoding='utf-8')[source]
class pycopia.WWW.XHTML.XHTMLDocument(dtd=None, doctype=None, lang=None, encoding='utf-8')[source]

XHTMLDocument is a complete XHTML document.

MIMETYPE = u'application/xhtml+xml'
add_javascript2head(text=None, url=None)[source]
add_stylesheet(url)[source]
add_stylesheets(namelist)[source]
add_title(title)[source]
append(obj, **kwargs)[source]
body
head
initialize()[source]
insert(ind, obj, **kwargs)[source]
javascript
next_id(name)[source]
scripts

Add a list of javascript file names.

style
stylesheet
stylesheets
title
pycopia.WWW.XHTML.add2class(node, name)[source]

Maintains space-separated class names.

pycopia.WWW.XHTML.check_flag(kwargs, name)[source]

enforce XML rules for flags.

pycopia.WWW.XHTML.check_object(obj)[source]
pycopia.WWW.XHTML.create_POM(data, dtd)[source]

Given a python object, produce reasonable markup from that. Return a valid POM node.

Dictionaries produce sections (divs) where key names are the class attribute name; lists produce ordered lists; Sets produce unordered lists; Tuples produce a fragment collection (invisible), an ElementNode is taken as-is, and strings return as Text nodes. Callables are called with the DTD, and the return value checked.

pycopia.WWW.XHTML.get_container(dtd, name, kwargs)[source]
pycopia.WWW.XHTML.get_document_class(doctype=None, mimetype=None)[source]
pycopia.WWW.XHTML.get_inlinecontainer(dtd, name, kwargs)[source]
pycopia.WWW.XHTML.get_url(params, base=None)[source]

Return a URL properly encoded for inclusion as an href. Provide a dictionary of parameters and an optional base path.

pycopia.WWW.XHTML.new_document(doctype='pycopia.dtds.xhtml11', mimetype=None, encoding='utf-8', lang=None)[source]
pycopia.WWW.XHTML.xhtml_factory(doctype=None, mimetype=None, encoding='utf-8', lang=None)[source]

Parsing for XHTML documents.

pycopia.WWW.XHTMLparse.get_document(url, data=None, encoding='utf-8', mimetype='application/xhtml+xml', useragent=None, validate=0, logfile=None)[source]

Fetchs a document from the given source, including remote hosts.

pycopia.WWW.XHTMLparse.get_parser(document=None, namespaces=0, validate=0, mimetype=None, logfile=None)[source]
pycopia.WWW.XHTMLparse.parseString(string)[source]

Document generation for HTML5. This is kind of a quick and dirty hack on top of the XHTML module. HTML5 does not have a DTD, so I use the existing XHTML transitional DTD for now.

class pycopia.WWW.HTML5.HTML5Document(dtd=None, doctype=None, lang=None, encoding='utf-8')[source]
MIMETYPE = u'application/xhtml+xml'
XMLHEADER = '<?xml version="1.0" encoding="UTF-8"?>\n'
initialize()[source]
set_doctype(doctype)[source]
pycopia.WWW.HTML5.new_document(encoding='UTF-8', language='en')[source]

Document generation for HTML5.

class pycopia.WWW.HTML5parse.ContainerMixin(encoding='UTF-8', language='en')[source]
class pycopia.WWW.HTML5parse.HTML5Document(encoding='UTF-8', language='en')[source]

HTML5Document represents the top-level document of an HTML5 document.

DOCTYPE = '<!DOCTYPE html>\n'
MIMETYPE = 'application/xhtml+xml'
XMLHEADER = '<?xml version="1.0" encoding="UTF-8"?>\n'
emit(fo, encoding='UTF-8')[source]
encode(encoding='UTF-8', pretty=False)[source]
initialize()[source]
set_encoding(encoding)[source]
pycopia.WWW.HTML5parse.get_document(url, data=None, encoding='UTF-8', mimetype='application/xhtml+xml', useragent=None, logfile=None)[source]

Fetches a document from the given source, including remote hosts.

pycopia.WWW.HTML5parse.get_parser(document=None, namespaces=0, strict=False, mimetype=None, logfile=None)[source]
pycopia.WWW.HTML5parse.new_document(mimetype=None, encoding='UTF-8', language='en')[source]
pycopia.WWW.HTML5parse.parse(filename_url_or_file, parser=None, encoding=None, useragent=None, accept=None)[source]

Parse a filename, URL, or file-like object into an HTML document tree. Note: this returns a tree, not an element. Use parse(...).getroot() to get the document root.

pycopia.WWW.HTML5parse.parseString(string)[source]

Client support

A web client based on pycurl library. Can also perform parallel requests. This can be used for testing, or automated clients.

Wrapper for pycurl that provides an easier to use, yet high performance HTTP client.

exception pycopia.WWW.client.BadStatusLineError[source]

Raised if the HTTP response status is un-parseable.

class pycopia.WWW.client.Client[source]
cookies[source]
get(url, logfile=None, query=None)[source]
post(url, data, logfile=None)[source]
class pycopia.WWW.client.ConnectionManager[source]

Manages HTTP, HTTPS, FTP connections

add_request(request)[source]
initialize()[source]
perform(logfile=None)[source]
class pycopia.WWW.client.DataWrapper(data)[source]
read(amt)[source]
class pycopia.WWW.client.DocumentAdapter(fp)[source]
read(size)[source]
exception pycopia.WWW.client.EncodingError[source]

Raised when trying to coerce a source to a unicode string and that fails. Indicates a badly encoded source.

exception pycopia.WWW.client.Error[source]
class pycopia.WWW.client.HTTPConnectionManager[source]

Manages multiple connections.

Response codes are obtained and number of retries can be set. It uses default proxy if proxies are not provided. Also, response object can be returned as response HTML/XML and Response POM.

add_request(request)[source]
get_URL(obj, logfile=None, cookiefile=None, **kwargs)[source]

Simple interface for making requests with uniform headers.

If you supply a string or URL object, it is taken as the URL to fetch. If you supply a list, multiple requests are issued. Returns two lists, as the perform() method does.

get_request(url, **kwargs)[source]

Convenient constructor method for getting a Request object.

perform(logfile=None)[source]

Fetch all of the added Request objects.

Return two lists. The first list is a list of responses that completed. The second is list of the requests that errored.

post_URL(obj, data, logfile=None, **kwargs)[source]

Perform a POST method.

class pycopia.WWW.client.HTTPForm[source]

A dictionary of HTTP form data.

Set an HTTPRequest’s data attribute to one of these to cause it to send a form with this object as contents. The values are maintained in a format that pycurl needs.

add_file(fieldname, pathname, mimetype=None, filename=None)[source]

Add a file section.

The file will be uploaded. You must use the full path name.

Args:
fieldname: name of the form field.
pahtname: Full path to the file. mimetype: Override the auto-detected mime type. filename: Override the base name of the given pathname.
from_form(formnode)[source]

Set the data from a Form object node (from WWW.XHTML module).

get_value(key)[source]

Get a value, without the pycurl form content type.

class pycopia.WWW.client.HTTPRequest(url, data=None, query=None, method='GET', encoding='utf-8', language='en', useragent='firefox_36_64', accept='text/xml, application/xml, application/xhtml+xml, text/html;q=0.9', extraheaders=None, strict=True, proxy=None, cookiejar=None, accept_encoding=None)[source]

General HTTP Requst object.

Arguments:
url : (None) String or UniversalResourceLocator object. data : (None) Dictionary of POST data. query : (None) Dictionary of extra URL query items. encoding : (DEFAULT_ENCODING) String indicating encoding request. language : (DEFAULT_LANGUAGE) String indicating language. useragent : (DEFAULT_UA) String for User-Agent header. accept : (DEFAULT_ACCEPT) String for Accept header. extraheaders: (None) List of extra headers. strict : (True) Parse URL string strictly. proxy : (None) Proxy host to use, if any. cookiejar : (None) A CookieJar object to get cookies from.
add_header(obj, value=None)[source]
clear_state()[source]
copy()[source]
data

Form data

del_query()[source]
encoding

Requested char encoding

get_URLencoded_poster()[source]

Initialze a Curl object for a single POST request.

Returns a tuple of initialized Curl and HTTPResponse objects.

get_deleter()[source]

Initialze a Curl object for a single DELETE request.

Returns a tuple of initialized Curl and HTTPResponse objects.

get_form_poster()[source]

Initialze a Curl object for a single POST request.

This sends a multipart/form-data, which allows you to upload files.

Returns a tuple of initialized Curl and HTTPResponse objects.

get_getter()[source]

Initialze a Curl object for a single GET request.

Returns a tuple of initialized Curl and HTTPResponse objects.

get_poster()[source]
get_query()[source]
get_raw_poster()[source]

Initialze a Curl object for a single POST request.

This sends whatever data you give it, without specifying the content type.

Returns a tuple of initialized Curl and HTTPResponse objects.

get_requester()[source]
get_uploader()[source]

Initialze a Curl object for a single PUT request.

Returns a tuple of initialized Curl and HTTPResponse objects.

headers

Headers to use

method

Request method

perform(logfile=None, cookiefile=None, retries=1, filename=None)[source]
proxy

Proxy

query

Update your query.

reset(url, data=None, query=None, method='GET', encoding='utf-8', language='en', useragent='firefox_36_64', accept='text/xml, application/xml, application/xhtml+xml, text/html;q=0.9', extraheaders=None, strict=True, proxy=None, cookiejar=None, accept_encoding=None)[source]
set_URL(url, strict=True)[source]
set_data(data)[source]
set_headers(headers)[source]
set_method(meth)[source]
set_proxy(value)[source]

Set a proxy in Request object to be used by Connection Manager.

set_query(val)[source]
url

Url Property

class pycopia.WWW.client.HTTPResponse(requested_encoding)[source]

Collects response, header, and body.

Provides properties for text and header objects. The doc attribute returns a parsed document.

body

The complete body of the response, as text string.

content

The complete body of the response, as text string.

cookielist

Netscape cookies.

doc

Parsed XHTMLDocument document.

error

Error information.

finalize(c)[source]

finalize a Curl object and extract information from it.

get_doc()[source]
headers

A list-like object containing HTTPHeader objects.

logfile

A write-able object that will get text from the fetch.

redirectcount

Redirect count.

responseline

A ResponseLine object with code, reason, and version.

savefile

File object to write response to.

set_logfile(fo)[source]
set_savefile(fileobject)[source]
status

A ResponseLine object with code, reason, and version.

timing

Timing information.

url

Effective URL.

exception pycopia.WWW.client.InvalidURLFormatException[source]

Raised when the URL supplied to the Request object is malformed.

class pycopia.WWW.client.Request[source]

Base class for all types of URL requests.

exception pycopia.WWW.client.RequestResponseError[source]

Raised if there was an error with a request, when you get the response.

class pycopia.WWW.client.ResponseLine(version, code, reason)[source]

HTTP response line container.

Attributes:
version: HTTP version as a float. code: HTTP Response code as an int. reason: HTTP response reason.
code
reason
version
class pycopia.WWW.client.TimingInfo(namelookup, connect, pretransfer, starttransfer, total, redirect)[source]
todict()[source]
pycopia.WWW.client.get_page(url, data=None, logfile=None, cookiefile=None, retries=1, filename=None, **kwargs)[source]

Simple page fetcher using the HTTPRequest.

Args:

url         : A string or UniversalResourceLocator object.
data        : Dictionary of POST data (of not provided a GET is performed).
logfile     : A file-like object to write the transaction to.
cookiefile  : Update cookies in this file, if provided.
retries     : Number of times to retry the transaction.
filename    : Name of file to write target object directly to.
              The doc and body attributes will not be available in the
              response if this is given.

Extra keyword arguments are passed to the HTTPRequest object.

Returns::
an HTTPResponse object.

Application framework

This is the server infrastructure, providing the FCGI servers and site controller. It natively supports virtual hosts (services based on the host name). It also provides a remote API for Javascript (client side) with JSON serialization.

exception pycopia.WWW.framework.Error[source]

Base framework error

class pycopia.WWW.framework.FrameworkAdapter(config)[source]

Adapt a WSGI server to a framework style request handler.

exception pycopia.WWW.framework.HTTPError[source]
code = None
message[source]
class pycopia.WWW.framework.HTTPRequest(environ)[source]
COOKIES
FILES
GET
POST
get_domain()[source]
get_full_path()[source]
get_host()[source]
has_key(key)[source]
headers
is_secure()[source]
log_error(message)[source]
exception pycopia.WWW.framework.HttpErrorLengthRequired[source]
code = 411
exception pycopia.WWW.framework.HttpErrorMethodNotAcceptable[source]
code = 406
exception pycopia.WWW.framework.HttpErrorMethodNotAllowed[source]
code = 405
exception pycopia.WWW.framework.HttpErrorNotAuthenticated[source]
code = 401
exception pycopia.WWW.framework.HttpErrorNotAuthorized[source]
code = 403
exception pycopia.WWW.framework.HttpErrorNotFound[source]
code = 404
exception pycopia.WWW.framework.HttpErrorServerError[source]
code = 500
exception pycopia.WWW.framework.HttpErrorUnsupportedMedia[source]
code = 415
class pycopia.WWW.framework.HttpResponse(content='', mimetype=None, charset='utf-8')[source]

A basic HTTP response, with content and dictionary-accessed headers

add_header(header, value=None)[source]
close()[source]
content
flush()[source]
get_response_headers()[source]
get_status()[source]
next()
status_code = 200
tell()[source]
write(content)[source]
class pycopia.WWW.framework.HttpResponseForbidden(content='', mimetype=None, charset='utf-8')[source]
status_code = 403
class pycopia.WWW.framework.HttpResponseGone(content='', mimetype=None, charset='utf-8')[source]
status_code = 410
class pycopia.WWW.framework.HttpResponseNotAcceptable[source]
status_code = 406
class pycopia.WWW.framework.HttpResponseNotAllowed(permitted_methods)[source]
status_code = 405
class pycopia.WWW.framework.HttpResponseNotAuthenticated(content='', mimetype=None, charset='utf-8')[source]
status_code = 401
class pycopia.WWW.framework.HttpResponseNotFound(content='', mimetype=None, charset='utf-8')[source]
status_code = 404
class pycopia.WWW.framework.HttpResponseNotModified(content='', mimetype=None, charset='utf-8')[source]
status_code = 304
class pycopia.WWW.framework.HttpResponsePaymentRequired(content='', mimetype=None, charset='utf-8')[source]
status_code = 402
class pycopia.WWW.framework.HttpResponsePermanentRedirect(redirect_to)[source]
status_code = 301
class pycopia.WWW.framework.HttpResponseRedirect(redirect_to, **kwargs)[source]
status_code = 302
class pycopia.WWW.framework.HttpResponseServerError(content='', mimetype=None, charset='utf-8')[source]
status_code = 500
exception pycopia.WWW.framework.InvalidPath[source]

Raised if a back-URL is requested by the handler that can’t be met by any registered handlers.

pycopia.WWW.framework.JSON404()[source]
pycopia.WWW.framework.JSONQuery(request)[source]

Convert query term where values are JSON encoded strings.

class pycopia.WWW.framework.JSONRequestHandler(flist, **kwargs)[source]

Sub-dispatcher for JSON requests. catches all exceptions and returns exception on error as JSON serialized objects (since async requests are not viewable on the client side).

Supply a list of functions to handle. The names of which match the “function” field in the URL mapping.

Your handler functions will get keyword arguments mapped from the request query or form. You return any Python primitive objects which get sent back to the client.

get(request, function)[source]
get_response(request, **kwargs)[source]
get_url(function)[source]
post(request, function)
class pycopia.WWW.framework.JSONResponse(obj)[source]

Used for asynchronous interfaces needing JSON data returned.

pycopia.WWW.framework.JSONServerError(ex, val, tblist)[source]
class pycopia.WWW.framework.RequestHandler(constructor=None, verifier=None)[source]
METHODS = [u'get', u'head', u'post', u'put', u'delete', u'options', u'trace']
delete(request, **kwargs)[source]
get(request, **kwargs)[source]
get_response(request, **kwargs)[source]
head(request, **kwargs)[source]
initialize()[source]
options(request, **kwargs)[source]
post(request, **kwargs)[source]
put(request, **kwargs)[source]
trace(request, **kwargs)[source]
class pycopia.WWW.framework.ResponseDocument(_request, _constructor=None, **kwargs)[source]

Wraps a text-creator document and supplies helper methods for accessing configuration.

anchor2(path, text, **kwargs)[source]
doc
finalize()[source]

Handlers should return the return value of this method.

get_icon(name, size=u'large')[source]
get_large_icon(name)[source]
get_medium_icon(name)[source]
get_object(key, ctor, **kwargs)[source]
get_small_icon(name)[source]
class pycopia.WWW.framework.URLAlias(regexp, method)[source]

Acts as an alias for static locations.

get_url(**kwargs)[source]
match(string)[source]
class pycopia.WWW.framework.URLMap(regexp, method)[source]

From regexp to url, and back again. Patterns must use named groups.

get_url(**kwargs)[source]
match(string)[source]
class pycopia.WWW.framework.URLRedirector(loc)[source]
class pycopia.WWW.framework.URLResolver(mapconfig=None, urlbase=None)[source]

Supports mapping URL paths to handler functions.

dispatch(request)[source]
get_alias(name, **kwargs)[source]
get_url(method, **kwargs)[source]

Reverse mapping. Answers the question: How do I reach the callable object mapped to in the LOCATIONMAP?

match(uri)[source]
register(pattern, method)[source]
unregister(method)[source]
pycopia.WWW.framework.default_doc_constructor(**kwargs)[source]
pycopia.WWW.framework.get_method(name)[source]

get a function from a module path.

pycopia.WWW.framework.parse_formdata(contenttype, post_data)[source]
pycopia.WWW.framework.redirectup(request, **kwargs)[source]

Master web site control. Handles virtual host setups using lighttpd as a front-end.

This controller also handles the lighttpd process itself. So if you have it enabled in your site’s init.d system you should disable it if they are configured to listen on the same port. The lighttpd server is run under the pycopia process manager and will be automatically restarted if it abnormally exits.

The pycopia web framework provides its own configuration file. Changes made to any installed lighttpd configuration won’t be used.

The lighttpd build was configured like this:

./configure –prefix=/usr –with-fam –with-openssl –with-attr –with-pcre –with-zlib –disable-ipv6

pycopia.WWW.website.check(config)[source]

Check the lighttpd configuration.

Provides transparent RPC between Javascript and Python, using JSON encoding.

pycopia.WWW.json.GetJSONDecoder()[source]
pycopia.WWW.json.GetJSONEncoder()[source]
class pycopia.WWW.json.GlobalRequest(request)[source]
class pycopia.WWW.json.JSONDispatcher(functions)[source]

Pair with PythonProxy javascript object.

Use this to call Python functions from javascript, with arguments and returns values transparently transported using JSON serialization.

Requires a URL mapping of the form:

(r'^base/path/(?P<methodname>\w+)$', 'views_module.dispatcher'),

Your view.py will then have a line like this:

_exported = [my_function]
dispatcher = json.JSONDispatcher(_exported)

The javascript client can then call an exported Python function with native parameters, and get native return value.

The javascript does this:

window.myproxy = new PythonProxy("/base/path/");

Your javascript now can do this:

  d = myproxy.my_function(1, "two");
  d.addCallback(myFunctionHandler);

If my_function returns 42, The myFunctionHandler gets 42.
Args:
functions: A list of function objects that are exposed to the client.
register_decoder(name, decoder)[source]
register_encoder(name, check, simplifier)[source]
class pycopia.WWW.json.JSONEncoder[source]
default(obj)[source]
register(name, check, simplifier)[source]
unregister(name)[source]
class pycopia.WWW.json.JSONNotFound(msg=None)[source]
class pycopia.WWW.json.JSONObjectDecoder[source]

Decoder for various complex objects.

Handles decoding complex objects defined by the JSON serialization protocol.

You can register new decoders for any object.

register(name, decoder)[source]
unregister(name)[source]
class pycopia.WWW.json.JSONServerError(ex, exval, tblist)[source]

Exports server side Python exception to browser client.

Web server using FCGI interface of lighttpd, adapted to WSGI.

pycopia.WWW.fcgiserver.ErrorHandler(exc_info, stream)[source]
class pycopia.WWW.fcgiserver.ProcessManager(pwent=None)[source]

Defines the process model the server uses.

pycopia.WWW.fcgiserver.check4server(config)[source]
pycopia.WWW.fcgiserver.get_server(config)[source]
pycopia.WWW.fcgiserver.kill_server(config)[source]
pycopia.WWW.fcgiserver.run_server(argv)[source]

Application middleware

Auth module

This is a primary authentication module for the framework. It implements a custom authentication scheme not found in other frameworks. It implements a CRAM-SHA1 authentication scheme to provide secure, password based authentication over a clear channel (plain HTTP). It has both Python and Javascript parts, so requires Javascript on the browser.

It also authenicates the user to the underlying OS using PAM. Therefore the web service acts as a proxy for the user account known to the underlying OS.

Upon successful authentication the client is provided a session cookie to be used for future requests to this server.

This enables reasonable security over plain HTTP. It is intended for intranet usage. It’s not 100% secure, so if more security is needed then use an encrypted channel, SSL (HTTPS). The framework does provide support, using lighttpd, for SSL. But using plain HTTP with this authentication scheme provides reasonable security without the hassle of managing SSL certificates.

One side effect of this scheme is that it requires a clear password to be stored in the servers database. This password is not actually stored in plaintext, but is encrypted using a master key. This master key is currently found in the auth.conf configuration file, which should be readable only by the root user. It should be possible to get this key from a secure source, such as a smartcard or TPM, but this is not currently implemented.

Table Of Contents

Previous topic

Pycopia XML package

Next topic

Pycopia QA package