Find file
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (97 sloc) 3.32 KB
layout title
default 0.3 API 0.3 API

Hello world

print will be replaced with return.

import web

urls = (
    '/(.*)', 'hello'
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: name = 'world'
        return 'Hello,', name+'!'

if __name__ == "__main__":


db configuration will not be global any more. Multiple databases can be used at the same time and no web.load() magic required to make database work.

import web

db = web.database(dbn='postgres', db='todo', user='you', pw='')'todo')'todo', where='id=$id', vars={'id': 2})
db.query('SELECT * FROM todo')


Application is a new way of mapping urls to classes, coming in 0.3. There will be many different kinds of supported applications.


Application to delegate requests based on path.

urls = (
    "/hello", "hello", 
    "/magic/.*", "magic")

app = web.application(urls, globals())


Application similar to web.application but urls are constructed automatiacally using metaclasses.

app = web.auto_application()

class hello(
    def GET(self):
        return "hello, world!"


Application to delegate requests based on subdir. This allows reuse of code easily by taking some exiting app and mounting it at a directory.

import wiki
import blog
import auth

mapping = (

app = web.subdir_application(mapping)


Application to delegate requests based on host. This makes virtual hosting very easy.

import mainsite
import usersite

mapping = (

app = web.subdomain_application(mapping)


Testing becomes very easy with applications. Both doctest and unittest can be used to test web applications.


urls = ("/hello", "hello")
app = web.application(urls, globals())

class hello:
    """Hello world example.

        >>> response = app.request("/hello")
        'hello, world!'
        >>> response.status
        '200 OK'
        >>> response.headers['Content-Type']
    def GET(self):
        web.header('Content-Type', 'text/plain')
        return "hello, world!"


import unittest
from helloworld import app

class HelloWorldTest(unittest.TestCase):
    def testHelloWorld(self):
        response = app.request('/hello')
        self.assertEquals(, 'hello, world!')
        self.assertEquals(response.headers['Content-Type'], 'text/plain')
        self.assertEquals(response.status, '200 OK')

if __name__ == "__main__":


  • no whitespace magic
  • better error reporting
  • should allow template reuse
  • Probably use Adam Atlas's implementation


  • New module, web.contrib with contributed utilities, which are not part of the core. For example, good auth module (port from django?) and OpenID support.