6.2 PSP Handler

PSP handler is a handler that processes documents using the mod_python._psp module. For more details on the PSP syntax, see Section 4.9.

To use it, simply add this to your httpd configuration:

  AddHandler mod_python .psp
  PythonHandler mod_python.psp

The PSP code will be given local variables req, psp, session and form. A session will be created and assigned to session variable only if session is referenced in the code (the PSP handler examines co_names of the code object to make that determination). Remember that a mere mention of session will generate cookies and turn on session locking, which may or may not be what you want. Similarly, a mod_python FieldStorage object will be instantiated if form is referenced in the code.

The object passed in psp is an instance of PSPInstance.

class PSPInstance()
set_error_page(filename)
Used to set a psp page to be processed when an exception occurs. If the path is absolute, it will be appended to document root, otherwise the file is assumed to exist in the same directory as the current page. The error page will receive one additional variable, exception, which is a 3-tuple returned by sys.exc_info().

apply_data(object[, **kw])
This method will call the callable object object, passing form data as keyword arguments, and return the result.

redirect(location[, permanent=0])
This method will redirect the browser to location location. If permanent is true, then MOVED_PERMANENTLY will be sent (as opposed to MOVED_TEMPORARILY).

Note: Redirection can only happen before any data is sent to the client, therefore the Python code block calling this method must be at the very beginning of the page. Otherwise an IOError exception will be raised.

Example:

<\%

# note that the '<' above is the first byte of the page!
psp.redirect('http://www.modpython.org')
\%>

If PythonDebug server configuration is On, then by appending an underscore ("_") to the end of the url you can get a nice side-by-side listing of original PSP code and resulting Python code generated by the psp module. This is very useful for debugging.

Note: Leaving debug on in a production environment will allow remote users to display source code of your PSP pages!

By default, compiled PSP pages are cached in memory. The cache is limited to 512 pages, which depending on the size of the pages could potentially occupy a lot of memory. If memory is of concern, then you can switch to dbm file caching. Our simple tests showed only 20% slower performance using bsd db. You will need to check which implementation anydbm defaults to on your system as some dbm libraries impose a limit on the size of the entry making them unsuitable. Dbm caching can be enabled via PSPDbmCache Python option, e.g.:

PythonOption PSPDbmCache "/tmp/pspcache.dbm"
Note that the dbm cache file is not deleted when the server restarts.