webenv — WSGI request, response and application abstractions.

class webenv.Application

WSGI application abstraction baseclass.

handler(request)

Method must be implemented by subclass.

Takes a Request instance and returns a Response instance.

Simplest usage:

from webenv import Application

class MyApplication(Application):
    def handler(self, request):
        if request.path == '/':
            return HtmlResponse(open('index.html', 'r').read())
class webenv.Request(environ, start_response)

Request object.

Environ keys are accessible using dictionary style syntax:

request['SERVER_PROTOCOL']
body
Body instance.
method

HTTP request method:

if request.method == "GET":
headers
Headers instance.
class webenv.Body(request)

Request Body Object.

str() must be called on request object to get a string:

body = str(request.body)
class webenv.Headers(request)

HTTP headers object.

Lookup headers using dictionary style syntax:

request.headers["accept-encoding"]

Note: make sure this is done caseless.

class webenv.Response([body])

HTTP Response object

status

HTTP status, must be full response string not an integer status code.

Default is “200 Ok”

content_type

HTTP content-type, default is “text/plain”.

A list of acceptable content-types is http://en.wikipedia.org/wiki/Internet_media_type

body
HTTP response body. If value has an iterator the iterator will be used in the response objects iterator.
add_header(name, value)

Add a header to the HTTP response:

response.add_header('Cache-Controler', 'no-cache')
class webenv.HtmlResponse(body)

HTML HTTP Response object. Subclass of Response.

Subclass of Response with content_type “text/html”.

body should be a string of HTML.

class webenv.FileResponse(file)

File wrapper for HTTP Responses. Subclass of Response.

If file is a str() or a unicode() object it is assumed to a path to a local filename and will be resolved. Any other type is assumed to be a File-like object.

The content-type is guessed by looking at the extension at the end of the requestion uri. You can override this by setting the Response.content_type.

readsize
Size of the read chunks for the response iterator. Default is 1024.
size
Optional attribute. If a filename is given to the constructor the file size will be used, if not size will not be set and the content-length header will not be set.
guess_content_type(path)
Guess the Response.content_type given a specific path.

webenv.rest — Resource based application abstractions.

class webenv.rest.RestApplication

REST Application.

Intended to be subclassed with the author defining methods GET(), POST(), PUT() and/or other HTTP methods.

GET(request[, *rest_args])

HTTP GET handler. Not implemented by baseclass, optionally implemented by subclass.

request is an instance of Request.

The request path is broken up and sent to rest_args:

class MyRestApplication(RestApplication):
    def GET(self, request, action, user, method=None):
        print action, user, method
        return Response()

Creates an application that prints:

>>> urllib2.urlopen("http://localhost/action1/testuser")
action1 testuser None
>>> urllib2.urlopen("http://localhost/a/testuser/another.txt")
a testuser another.txt
PUT(request[, *rest_args])

HTTP PUT handler. Not implemented by baseclass, optionally implemented by subclass.

Usage identical to GET().

POST(request[, *rest_args])

HTTP POST handler. Not implemented by baseclass, optionally implemented by subclass.

Usage identical to GET().

add_namespace(name, rest_application)

Add a REST application to a specific namespace.

This method allows you to reuse RestApplication subclasses:

class PagesApplication(RestApplication):
    def GET(self, request, *args):
        print "Pages::", args
        return Response()

class IndexApplication(RestApplication):
    def GET(self, request, *args):
        print "Index::", args
        return Response()

index_application = IndexApplication()
index_application.add_namespace("pages", PagesApplication())

And now index_application will print:

>>> urllib2.urlopen("http://localhost/blah")
Index:: ('blah')
>>> urllib2.urlopen("http://localhost/pages/aa/bb/cc")
Pages:: ('aa', 'bb', 'bb')

webenv.applications — Prebuilt webenv applications.

class webenv.applications.FileServerApplication(basepath)

File serving application.

basepath is an absolute path to the base directory to be served.

Is a subclass of webenv.rest.RestApplication so it can be added as a namespace:

class MyApplication(RestApplication):
    def GET(self, request):
        return FileResponse("/var/static/index.html")

application = MyApplication()
application.add_namespace("css", FileServerApplication("/var/css"))
application.add_namespace("images", FileServerApplication("/var/images"))