ModPython: passing variables between handlers?

Andrew James drew at gremlinhosting.com
Thu Feb 3 08:20:58 EST 2005


Hi all,
I'm writing an application which runs within Apache and uses mod_python 
to provide basic authentication and return content to the user, 
something like:

import modpython
....
def handler():
   # main part of the application starts here
   ...

def authenhandler():
   ...
   # Store information about the user in an object
   u = new User(req.user, pass)

I'd like to be able to pass the u object somehow from authenhandler to 
handler in an elegant fashion, but without using global variables (as I 
understand it these persist for the life of the child process which will 
be more than one request, which is not what I want, and could be a 
security hole).

I suppose that I could use session variables, but since this part of my 
application provides a WebDAV server, basic authentication credentials 
are passed on each request (so I don't really want to have to look after 
keeping track of sessions when I don't have to). I would rather not 
modify all my existing classes to support an  extra parameter in their 
constructors.

What I'm really looking for is some sort of global dictionary like PHP's 
$REQUEST or $SESSION, which I can assign freely to during the life of 
the request *from anywhere in my application* and which gets cleaned up 
for me automatically afterwards. Does something like this exist in 
mod_python?

If the approach above isn't possible, what would your recommendations be 
for a solution to this issue?

Many thanks for your time,
Andrew James



More information about the Python-list mailing list