[mod_python] How do I use PythonTransHandler?

Ian Clelland ian at veryfresh.com
Sat Aug 17 03:36:06 EST 2002

On Sat, Aug 17, 2002 at 08:37:43PM +1200, Alex King wrote:
> > The problem I can see right now is that there appears to be no way to
> > get the DocumentRoot for the current request. You shouldn't have a 
> > problem if your cached files are completly outside of your DocumentRoot 
> > (and you have the absolute path available to your handler,) but you 
> > might have a hard time if you want to translate a URL to another file 
> > within your DocumentRoot, and don't want to put the full path inside 
> > your source files.
> Yeah, I discovered that.  I was looking at req.get_config() to see what
> it returned, it only seems to return module specific configs.  If it
> returned all the configs I would have picked DocumentRoot out of that.
> Instead I'm going to ignore DocumentRoot and set various directories to
> be used for the cache etc. with PythonOption directives in the apache
> config.

That's what I'd suggest -- if you really need to use your document root,
then add a PythonOption with the same value to your server config. I
looked at get_config (and the less-documented get_all_config) and it
only gets the configuration directives which are specific to mod_python.

mod_perl seems to have a document_root() method for its request object;
I haven't looked too closely at how it gets that from Apache, though. It
would be a nice addition to mod_python (hint, hint :) )

> Another problem I'm having is not understanding req.add_handler().  I
> thought I'd have code to determince whether to take special action in
> the PythonTransHandler (ie. download a file not in cache).  If a
> download is needed, the transhandler runs
> req.add_handler("PythonHandler","padcache") where padcache.py is my
> script in /usr/local/lib/python2.1/site-packages/.
> I'm probably not understanding the second argument to the function, or
> perhaps it's a path problem again, but I keep getting a traceback:
> [Sat Aug 17 20:29:48 2002] [error] PythonHandler padcache: Traceback (most recent call last):
> [Sat Aug 17 20:29:48 2002] [error] PythonHandler padcache:   File "/usr/lib/python2.1/site-packages/mod_python/apache.py", line 176, in Dispatch
>     dir = _req.get_all_dirs()[htype]
> [Sat Aug 17 20:29:48 2002] [error] PythonHandler padcache: KeyError: PythonHandler

Try using req.add_handler("PythonHandler", "padcache::handler"). I'm not
sure where the "::" notation comes from -- it appears to be internal to
mod_python, or perhaps to python itself. Either way, I think you need to
specify both the module name (padcache) and the handler function
(usually just 'handler' for content handlers).

Note: I haven't tried this one myself; that's just what I can glean from
the documentation.

> Thanks for your quick response :)

No problem -- but now I've got to get some sleep :)


<ian at veryfresh.com>

More information about the Mod_python mailing list