[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
</Directory>

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
elsewhere.

#!/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.send_http_header()
        req.write("Hello World!\n")
        req.write('uri:'+req.uri+'\n')
        req.write('filename:'+req.filename+'\n')
        req.write('path_info:'+req.path_info+'\n')
        if req.args: req.write('args:'+req.args+'\n')
    else:
        req.content_type = "text/html"
        req.send_http_header()
        req.write(page(req.filename[len(homedir):]))
    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