WSGI application abstraction baseclass.
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())
Request object.
Environ keys are accessible using dictionary style syntax:
request['SERVER_PROTOCOL']
HTTP request method:
if request.method == "GET":
Request Body Object.
str() must be called on request object to get a string:
body = str(request.body)
HTTP headers object.
Lookup headers using dictionary style syntax:
request.headers["accept-encoding"]
Note: make sure this is done caseless.
HTTP Response object
HTTP status, must be full response string not an integer status code.
Default is “200 Ok”
HTTP content-type, default is “text/plain”.
A list of acceptable content-types is http://en.wikipedia.org/wiki/Internet_media_type
Add a header to the HTTP response:
response.add_header('Cache-Controler', 'no-cache')
HTML HTTP Response object. Subclass of Response.
Subclass of Response with content_type “text/html”.
body should be a string of HTML.
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.
REST Application.
Intended to be subclassed with the author defining methods GET(), POST(), PUT() and/or other HTTP methods.
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
HTTP PUT handler. Not implemented by baseclass, optionally implemented by subclass.
Usage identical to GET().
HTTP POST handler. Not implemented by baseclass, optionally implemented by subclass.
Usage identical to GET().
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')
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"))