ModPython: passing variables between handlers?

A

Andrew James

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
 
S

Steve Holden

Andrew said:
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
 
D

Diez B. Roggisch

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top