[mod_python] mod_python, best practices

Graham Dumpleton grahamd at dscpl.com.au
Wed Nov 24 05:36:39 EST 2004


On Wednesday, November 24, 2004, at 08:35  PM, Rune Hansen wrote:

> Hi,
> I've written a rather long and winding document which I've called 
> "mod_python, best practices".
>
> A better name would be "mod_python, assumptions made on shaky ground - 
> kept in a light tone to mask my inherent fear of making a complete 
> fool of my self." Obviously, that title is to long.
>
> Anyway, the document can be found here http://www.scanmine.com/mp/
>
> I'd really like thoughts, comments and corrections... keep your smirks 
> and laughs to your self :)
>
> As you read the document it will become apparent that english is not 
> my native tongue, feel free to comment on that too.

You sent this at a really bad time as am just about to go on week and
a half holiday and will not have internet access. I also don't have
the time now to respond to your posting properly. A few quick points
though.

On the first issue, you really seem to complicate things using the
RewriteRule directive. Have a look at Vampire and how it works.
You should be able to avoid the rewrite rule using Vampire as it
will automatically map a "search.html" request to "search.py".
You will have to write your handlers in the default content handler
style and not mod_python.publisher style though. Thus something
like:

# index.py

from mod_python import apache

def handler_html(req,**args):
     req.content_type = "text/html"
     req.send_http_header()
     req.write("<html><head><title>Search Index 
Page</title><head><body>## 'index' got called once</body></html>")
     return apache.OK

# search.py

from mod_python import apache

def handler_html(req,**args):
     req.content_type = "text/html"
     req.send_http_header()
     req.write("<html><head><title>Search /search 
Page</title><head><body>## '/search' got called once</body></html>")
     return apache.OK

Not sure what may happen if PythonAuthenHandler is used though and
whether you will still see excessive calls to it.

On the issue of authorisation, when using mod_python.publisher did
you look at the the __auth__, __auth_realm__ etc support it provides
whereby authorisation could be handled in each Python code file
rather than by PythonAuthenHandler? Vampire provides the same sort
of authentication hooks in each content handler file.

On the issue of configuration files, again, look at Vampire. It
provides a mechanism for configuration which also uses ConfigParser.
It has though a smart search ability to find the config file. Ie.,
rather than having to specify the exact location of the config file,
you simply ask for config file of certain name, and using the req
object will search back up the directory hierarchy until found,
or will stop searching if go up as far as where the PythonHandler
definition was found. Thus instead of writing:

def handler(req):
     global CONFIG,cfgfile
     if not CONFIG:
        CONFIG = 
Cfgparser("/".join([os.path.dirname(__file__),cfgfile]),req.server)
        CONFIG.getConfig()
     req.CONFIG = CONFIG

one can write something like:

def handler_html(req):
     config = vampire.loadConfig(req,"Globals.cfg")

This means that you don't have to specify fixed relative pathnames in
subdirectory content handlers to refer to a config somewhere above
that directory, instead it will be automatically found. This allows
one to freely move stuff around and not have to fix up any fixed
location paths.

The config object you get back also has some default values set which
allow one to easily create URLs which reference back to the root of
your application, again avoiding issues of having hardcoded relative
paths. Unfortunately, haven't yet released a newer version of Vampire I
have which takes this a bit further and makes it even more useful.

Configs are cached automatically, but the cache will also reload them
automatically when it detects that the actual config file has changed.

Anyway, could probably go on longer and provide better examples, but
no time to do so. I realise the above doesn't adequately explain stuff
either, so all I can suggest is you have a dig through Vampire and
see what you can work out and I'll see where things are at when I get
back from holidays.

Vampire can be found at:

   http://www.dscpl.com.au/projects/vampire

--
Graham Dumpleton (grahamd at dscpl.com.au)



More information about the Mod_python mailing list