ModPython: passing variables between handlers?

Steve Holden steve at holdenweb.com
Thu Feb 3 08:31:24 EST 2005


Andrew James wrote:

> 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?
> 
RTFM ;-)

If you want these values to have the same lifetime as your requests then 
it would appear to make sense to have them be request attributes, no?

Section 4.5.3 of the mod_python docs says little else about the request 
object but """You can dynamically assign attributes to it as a way to 
communicate between handlers.""".

regards
  Steve
-- 
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/



More information about the Python-list mailing list