4.3 Using PythonWeb

Hopefully by now you have a functioning web server that is producing output to your web browser. You are now ready to start learning PythonWeb.

If you haven't already installed the web modules you should do so now. There are instructions in the Getting Started guide. Alternatively you can use sys.path to tell Python where the modules are installed.

Here is the same Hello world! example written with PythonWeb.

#!/usr/bin/env python

import sys; sys.path.append('../dir')
import web
print web.header()
print "<html>Hello world!</html>"

Agian you may have to change the first line to point to the Python interpreter and you should change '../dir' to point to the directory containing the web directory as described in the installation section.

The next example prints a list of all the CGI variables your server is making available to CGI scripts.

#!/usr/bin/env python

import sys; sys.path.append('../dir')
import web
print web.header('text/plain')
for key, value in web.cgi.items():
    print "%s: %s\n"%(key, value)

Note how this time we are sending the information as plain text using an HTTP header web.header('text/plain').

Once you have this working you are ready to begin reading the Module Reference and looking at the examples in the next section.