[mod_python] Newbie question: setting all calls to a certain directory to beprocessed by a script

Russell Nelson nelson at crynwr.com
Wed Aug 27 22:23:27 EST 2003

Simon Willison writes:
 > my choice. If that isn't possible, I'd like to set it up so that all 
 > requests within the directory in which my .htaccess file lives are 
 > handled by the one handle() function.

Simon, Simon, Simon, where were you last week?  I could have read the
answer to your question, and avoided having to post my question
earlier this week.  It's pretty easy.  Do it like this:

<Directory "/var/www/ae.russnelson.com">
SetHandler python-program
PythonHandler publish

The SetHandler says that everything should be handled by the
python-program.  The PythonHandler says that the name of the script is
publish.py.  Here's a simple publish.py.  I'll be publishing a more
complicated example later once I finish porting my blogging code over
to mod_python.  It's currently stand-alone code that writes static
HTML pages.  The astute observer will notice the constant "if 1" whose
unreachable clause calls page().  That code returns an HTML document
given the pathname that the user handed in.  Note that you'll never
get the null string, but instead 'index.html' or as configured

#!/usr/bin/env python

from mod_python import apache
import sys, os, re, string

homedir = "/var/www/ae.russnelson.com/"

def handler(req):
    if 1:
        req.content_type = "text/plain"
        req.write("Hello World!\n")
        if req.args: req.write('args:'+req.args+'\n')
        req.content_type = "text/html"
    return apache.OK

--My blog is at angry-economist.russnelson.com  | Free markets express in the
Crynwr sells support for free software  | PGPok | practical world our belief
521 Pleasant Valley Rd. | +1 315 268 1925 voice | that there is that of God
Potsdam, NY 13676-3213  | +1 315 268 9201 FAX   | in all people. -Chris V.

More information about the Mod_python mailing list