[mod_python] How do I use PythonTransHandler?

Ian Clelland ian at veryfresh.com
Sat Aug 17 00:44:09 EST 2002

On Sat, Aug 17, 2002 at 04:29:15PM +1200, Alex King wrote:
> Quoting a previos post, by Ian Clelland:
> >PythonTransHandler, which will receive all requests, and has the chance 
> >to modify the URL before Apache passes the request to your main
> >handler.
> How do I use this?
The same way you would set a regular PythonHandler, you can set up 
Apache to use a PythonTransHandler:

<VirtualHost *:80>
  ServerName pytest.zoostation
  DocumentRoot /var/local/apache/htdocs/pytest
  PythonPath "sys.path+['/home/ian/src/pytest']"
  PythonTransHandler translate_test

And then just write the python module to set the filename attribute:

def transhandler(req):
    req.filename = '/var/local/apache/htdocs/pytest/test2.html'
    return apache.OK

> My problem is that the request object members such as filename and the
> like are read-only, so how do I pass the new filename back to apache?

Well, req.filename is definitely _not_ read-only. This may be another
bug in the documentation, but the code is there in requestobject.c to
set that attribute. I just ran a couple of tests, and I can set the
filename for a request, and Apache will return that file.

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.

Ian Clelland
<ian at veryfresh.com>

More information about the Mod_python mailing list