Pesto is a library for writing web applications in Python:
- Query WSGI request parameters.
- Map any URI to any part of your application.
- Easily set and manipulate response headers - cookies, redirects and more.
- Produces 100% WSGI applications
A very basic script to run in a CGI environment:
#!/usr/bin/python
import pesto
from pesto import Response
def handler(request):
return Response([
"<html>",
"<body><h1>Whoa Nelly!</h1></body>",
"</html>",
])
if __name__ == "__main__":
app = pesto.to_wsgi(handler)
pesto.run_with_cgi(app)
Using the urldespatcher mechanism to map URLs to handlers, running through Python's wsgiref server:
#!/usr/bin/python
from wsgiref import simple_server
import pesto
from pesto import Response
despatcher = pesto.urldespatcher()
recipes = {
'baked-beans' : "Open a tin of baked beans. Put into a saucepan, heat and serve.",
'toast' : "Put a slice of bread under a grill for 2-3 minutes, turning once. Serve.",
}
@despatcher.match('/', 'GET')
def recipe_index(request):
"""
Display an index of available recipes.
"""
return Response(
[
'<html><body><h1>List of recipes</h1><ul>'
] + [
'<li><a href="%s">%s</a></li>' % (
show_recipe.url(recipe=recipe), recipe
) for recipe in recipes
] + [
'</ul></body></html>'
]
)
@despatcher.match('/recipes/recipe:.*', 'GET')
def show_recipe(request, response, recipe):
"""
Display a single recipe
"""
return Response([
'<html><body><h1>How to make %s</h1>' % recipe,
'<p>%s</p><a href="%s">Back to index</a>' % (recipes[recipe], recipe_index.url()),
'</body></html>'
])
if __name__ == "__main__":
app = pesto.despatcher_app(despatcher)
app = pesto.to_wsgi(app)
httpd = simple_server.make_server('', 8080, app)
httpd.serve_forever()
Pesto's Google Code site contains the latest releases.
Pesto is production ready, and used on a wide variety of websites.
Pesto is available under the terms of the new BSD licence.