[mod_python] Session variables

Terry MacDonald terry.macdonald at dsl.pipex.com
Sun Apr 18 23:21:24 EST 2004


Hi all,

Here is a working example of a function using the session class and
setting some session variables.  This is an authentication handler and
the variables will be available to the main handler via the
request.session variable.

There is a quirk where the req.session.unlock() needs to be called to
release the session for the following handler. I thought this would have
been done in the session code, so could be a bug. The docs state
themselves that lock and unlock are unlikely to be called by apps,
however, I have found this not usually the case.

======================================================================
def authenhandler( req ) :
    """ This handler is only called if the AuthName, AuthType and
        Requires apache directives are set for a directory """

    status = apache.OK
    # Get the session id from a cookie or create a new session. If it's
    # not new the user must already have been authenticated
    req.session = Session.Session(req, secret="xxxxxxxx")
    if req.session.is_new() :
        # Get user details...
        user = _dbGetUser(req, req.user)
        if user :
            # Authenticate user...
            if user[0] == req.get_basic_auth_pw() :
                # Store team id and name with the new session
                req.session['tid'] = user[1]
                req.session['team'] = user[2]
                req.session.save()
                req.log_error( "%s logged in (session=%s)" %
(req.session['team'], req.session.id()), apache.APLOG_NOTICE )
            else:
                req.log_error( "Invalid password for %s" % req.user,
apache.APLOG_NOTICE )
                status = apache.HTTP_UNAUTHORIZED
        else:
            status = apache.HTTP_UNAUTHORIZED
    req.session.unlock()
    return status
==========================================================================

Hope this helps.

Anyone have any answers to a previous email I sent entitled 'index.py'.
I mistakenly added it to another unrelated thread when, in fact, it
should be its own thread.

Cheers
-- 
Terry
Registered Linux User # 311806
www.taumu.com


> On Sun, 2004-04-18 at 21:45, John Draper wrote:
> On Apr 1, 2004, at 8:58 AM, jakob at simon-gaarde.dk wrote:
> 
> > Hello.
> > I can see that you can save and load session values from mod_python 
> > 3.1,
> > but unfortunately I don't understand how this works. Could someone
> > elabourate with an example please?
> 
> You're not the only one looking for examples.   I'm trying to put 
> together
> some examples myself,  but so far,  none of my code is working.  
> Probably
> due to the fact that none of us can read the author's mind and figure
> out much of anything,  because there is no clue we can use to guide us
> on how it's all supposed to be toed together.    Hopefully,  someone
> might beat me to it.
> 
> John
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python




More information about the Mod_python mailing list