[mod_python] newbie question - using multiple handlers

Graham Dumpleton grahamd at dscpl.com.au
Tue Jul 25 21:22:10 EDT 2006


Martijn Moeling wrote ..
> 
> I would do it different
> 
> 
> I would do something like this:
> 
> One handler in the apache config
> 
> Def handler(req):
> 	If req.unparsed_uri == "/page1.html":
> 		#Do something for page1
> 		Page=1
> 	elif req.unparsed.uri == "/page2.html":
> 	      #Do something for page2
> 		Page=2
> 	Send_page(req,Page)
> 	Return Apache.Ok
> 
> Def Send_page(req,Page):
> 	req.write("this is page : "+str(Page))
> 
> (My outlook messes up the use of small and capital letters, you get the
> point though

Which just like Vampire is just another switch or dispatch handler which is
what they wanted to avoid. At least with Vampire it is a generic dispatch
handler whereas the above would require modification every time a new
page is added.

A few other comments on your example above.

First is that it will only work if the handler is located at the root of the URL
namespace. A dispatch handler should base decisions on the relative URL
which is situated below the point in the URL hierarchy at which the handler
is anchored. This is not necessarily what req.path_info is set to and needs
to be calculated properly as a separate step.

Second is that because the handler runs as the response handler and you
always return apache.OK for everything, it isn't possible to let Apache fall
back to serving up a static file with the same extension as any request being
processed through the handler.

It is possible to return apache.DECLINED when the handler doesn't handle
the URL and then Apache will return either NOT FOUND or serve up any
matching static file. This will only work for static files though and it isn't
possible to defer to a completely different Apache module to deliver up a
dynamic response using a handler of its own.

For it to work with non static output, would require a fixuphandler to be
used to make the decision rather than SetHandler or AddHandler and for
enabling of mod_python to be dynamic. This though also requires
mod_python 3.3 as prior versions of mod_python don't allow req.handler
to be set by handlers.

In short, in this case I also believe one is better off using a dispatch handler,
but I would highly recommend an existing generic solution rather than a
hand rolled solution as there are lots of gotcha to doing it right. I would
also say that Apache does have a lot of configurability which can also be
used, and just because we are Python fanatics here doesn't mean we should
throw away all the goodness Apache has to offer and try to do everything
in a Python response handler. ;-)

Graham

> -----Oorspronkelijk bericht-----
> Van: mod_python-bounces at modpython.org
> [mailto:mod_python-bounces at modpython.org] Namens Graham Dumpleton
> Verzonden: Wednesday, July 26, 2006 00:12
> Aan: Thomas J. Schirripa
> CC: mod_python at modpython.org
> Onderwerp: Re: [mod_python] newbie question - using multiple handlers
> 
> Thomas J. Schirripa wrote ..
> > I'm pretty new to both mod_python and apache. I have just written
> multiple
> > handlers to deal with different webpages I have written. My problem is
> > that if I use PythonHandler for each of my handlers in the conf. file,
> > whenever I run a webpage, it tries to use ALL my handlers. Basically,
> I
> > want only one handler to be used per webpage. The "action" in my forms
> > only specify one handler, but based on the errors I am getting, it is
> also
> > running the other handlers. Is there a way that I can set directives
> such
> > as PythonHandler for particular html files? Or do I need to separate
> my
> > files into different directories? I would hate to have to make a
> switch
> > handler that determined what methods to call from my handler files so
> that
> > the conf file only had one handler in it.
> 
> Post what Apache configuration you are using for your PythonHandler
> and SetHandler/AddHandler directives related to mod_python. Is this
> being set in a .htaccess file or in main Apache configuration within a
> Directory/Location directive?
> 
> If the PythonHandler directives at present refer sto your own handlers,
> use
> of a switch or dispatch handler may be your only choice when using
> mod_python
> 3.2.X or earlier versions. This is because the logical thing to do of:
> 
>   <Files page1.html>
>   PythonHandler handlers::page1_html
>   </Files>
> 
>   <Files page2.html>
>   PythonHandler handlers::page2_html
>   </Files>
> 
> doesn't necessarily work, although it is fixed in development version of
> mod_python 3.3.
> 
> The problem with using <Files> is that mod_python sets up Python path
> wrongly and if handlers.py is in the directory for the .htaccess or
> Directory
> directive it will not find it. Will be okay if handler.py is elsewhere
> and the
> PythonPath directive is used to refer to the directory where it is
> located.
> 
> Rather than try and hack up a solution you may want to look at Vampire
> which already provides a basic switch or dispatch handler which maps
> to basic mod_python handlers. This way you can have separate handler
> files for each resource and if necessary distinct handlers for different
> views of a resource as well. See:
> 
>   http://www.dscpl.com.au/projects/vampire/articles/vampire-001.html
> 
> for a quick overview of Vampire.
> 
> Graham
> 
> 
>   
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python


More information about the Mod_python mailing list