ModPython: passing variables between handlers?
Diez B. Roggisch wrote: Hi, first of all - please quote appropriately - especially if you receive the postings as digest. That prevents the signal-to-noise-ratio dropping to unknown lows... Your responses will give me the required lifetime for my variables, but not the required access at all levels of code (unless req is a global variable?). Do you have any other suggestions as to how this might be implemented? If the code you showed us _is_ what you use - then req seems to be a global variable. Otherwise it won't work. Yes, I've changed the subject line to try and avoid too much further confusion. Andrew, you are aware, I take it, that the request object is provided as an argument to each handler that's called for a request? Using any kind of global in mos_python code is dangerous because of the potential for one request's changes to overwrite another's. I've had to deal with similar complications in my own code, but you may want to think about biting the bullet. Globals are almost always a bad idea in multi-user contexts like this. 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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: ModPython: passing variables between handlers?
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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: ModPython: passing variables between handlers?
> 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) > 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? I have absolutely no experience with mod_python, so take this with a grain of salt - but you code above suggests that there is a request object: req. You already use it: req. How about storing u in req like this: def authenhandler(): # Store information about the user in an object req.u = new User(req.user, pass) -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
ModPython: passing variables between handlers?
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 -- http://mail.python.org/mailman/listinfo/python-list