[mod_python] How do I use PythonTransHandler?

Alex King alex at morrison.king.net.nz
Sat Aug 17 20:37:43 EST 2002


On Sat, Aug 17, 2002 at 12:44:09AM -0700, Ian Clelland wrote:
> 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']"

ahh - that's how you do it.  I expected the DocumentRoot to be added to
the sys.path by mod_python, but it wasn't  I ended up putting my script
in /usr/local/lib/python2.1/site-packages/ to get it in the path.  I
overlooked PythonPath for some reason...

>   PythonTransHandler translate_test
> </VirtualHost>
> 
> 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.
> 
OK, cool that's how I expected it to work.  The read-only note beside
the req.filename in the docs put me off though and I didn't actually try
it.  Perhaps the request filename is read-only by the time it get to the
main request handler?  In the docs I have, all the members of the rquest
structure are noted to be read-only except for content_type, headers_in,
headers_out, and err_headers_out.  Perhaps those read-only notes in the
docs are all in relation to the main request handler, not to all
handlers in general.

Anyway this makes me much happier :)

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

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

What I am wanting to achieve is to normally not have a python handler
run for the main handler, so no PythonHandler is specified in the
config, but in cases when transhandler determines that it does need to
be run, it is added with add_handler()

> 
> 
> Ian Clelland

Thanks for your quick response :)

Alex



More information about the Mod_python mailing list