[mod_python] Modifying publisher for sensible URLs

David Higgs drh9296 at ritvax.rit.edu
Fri Sep 6 11:35:18 EST 2002


> Could we discuss whether it were worth to change the publisher module so
> that it offers some more flexibility as far as URLs are concerned? I'm afraid I
> just don't know enough about the publisher to do some more serious
> modifications. However, I've found some problems related to this issue:
> 
> 1. "http://servername/path/script/" works, but
> "http://servername/path/script" (without the trailing slash) doesn't. (I can imagine why, but don't know
> how to fix it)

That functionality is already possible if you roll your own handler. 
Depending on the amount of functionality you want, you may only need 20 
lines of code (or less) to parse and dispatch the request.

With the setup below, http://www.domain.com/util will be a call to 
util_handler.py.  The 'main' method called is always handler().  You'll 
use request.filename and request.path_info to determine what what 
requested.  In this case, the filename would be 
/path/to/your/script/util/index.html, and the path_info would be an 
empty string.

Here's an approximation of my setup:

<Location /util>
     Options -Indexes
     SetHandler python-program
     PythonPath "sys.path+['/path/to/your/script/util', 
'/path/to/your/modules']"
     PythonHandler util_handler
</Location>

bash$ cd /path/to/your/script/util/
bash$ ls
index.html      util_handler.py

The annoying part is that you *need* the index.html to prevent a 404 
error from happening before your script is called.  The content of 
index.html doesn't matter since it is never read; mine are all 
zero-length files.

I have also found that with multiple PythonHandlers, each script must be 
named differently to prevent caching issues.  This wasn't immediately 
obvious, since each httpd thread seemed to have a separate cache.

--david




More information about the Mod_python mailing list