[mod_python] TransHandler help?

Robert Sherwood foofboy at speakeasy.net
Tue Jan 8 11:38:23 EST 2002


Hi,

Transhandler translates a request from a URL to a filename. Though you
can rewrite a URL, I found it was easier simply to use the URL that I
had, particularly since, In my case, I could count on the argument order
and such.

This is an extremely simple example, but I wanted to set the file
request to main.py, which handled all the URL processing itself. The
important thing to remember is that the TransHandler figures out which
file you're requesting based on the URL you pass in. It can obviously do
any processing you need, but that is it's primary function.

from mod_python import apache
import string

def transhandler(req):
    #We want to use main.py
    req.filename = "main.py"
    return apache.OK

Again, I didn't use mod_python to process the URL, that is done by the
main Python Handler, as below (adapted slightly for your situation):

def handler(req):
    #remove trailing slash, if present
    URI = req.uri
    if URI[len(URI)-1] == '/':
        URI = URI[:len(URI)-1]

    #disassemble the URI for processing
    components = string.split(URI,'/')[1:]

    sessionID = components[0]

    command = components[1]

    what = components[2]

    # Do stuff here

There's no error checking or anything, but I found it was easier to use
the request as it came in, rather than rewriting it and processing that.
However, I had to use the TransHandler to set the request filename to
the script that did the processing.

Hope this helps.

Rob



On Mon, 2002-01-07 at 19:10, Gregory (Grisha) Trubetskoy wrote:
> 
> I'll have look into this some day. I *think* what mod_rewrite does is
> create a sub-request, but sub-requests are not supported by mod_python (i
> don't think there is anything that makes it impossible, the code is simple
> not there, it's just a SMOP ("simple matter of programming")...)
> 
> Grisha
> 
> 
> On Mon, 7 Jan 2002, Alan Kennedy wrote:
> 
> > Thanks Olly,
> >
> > It's good to know that I'm not the only who can't get it to work :-\
> >
> > I've been avoiding relying on mod_rewrite and its "cool voodoo". I have the training and
> > background to get my head around it, but members of my team unfortunately don't, whereas
> > they are all very comfortable with python.......
> >
> > Thanks for the reply,
> >
> > Cheers,
> >
> > Alan.
> >
> > Oliver Cope wrote:
> >
> > > On Mon, Jan 07, 2002 at 07:32:10PM +0000, Alan Kennedy wrote:
> > > >
> > > > I'm trying to get a PythonTransHandler working.
> > > >
> > > <snip>
> > >
> > > > *without* sending a redirect back to the client. Or a more sophisticated
> > > > example, how could I turn a request for
> > > >
> > > > /admin/abc1234/update/item4567
> > > >
> > > > into a request for
> > > >
> > > > /admin/doit.html?sessionID=abc1234&command=update&what=item4567
> > > >
> > > I too have spent some time trying to configure a PythonTransHandler, but
> > > couldn't get it to work :o(
> > >
> > > I've had more luck using mod_rewrite for this kind of thing:
> > >
> > > RewriteEngine On
> > > RewriteRule /admin/(.*)/(.*)/(.*) /admin/doit.html?sessionID=$1&command=$2&what=$3 [PT]
> > >
> > > This will rewrite requests without sending redirects back to the client.
> > > You can remove the '[PT]' if you don't need to further process the
> > > rewritten URL (e.g. through a PythonHandler).
> > >
> > > Obviously regular expressions are not as flexible as a Python
> > > handler can be, so this will only work for simple translations.  Also
> > > you'll need to have Apache compiled with mod_rewrite, I'm not sure if
> > > this is the default.
> > >
> > > Regards,
> > >
> > > Olly.
> > >
> > > _______________________________________________
> > > Mod_python mailing list
> > > Mod_python at modpython.org
> > > http://www.modpython.org/mailman/listinfo/mod_python
> >
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://www.modpython.org/mailman/listinfo/mod_python
> >
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python
> 





More information about the Mod_python mailing list