[mod_python] Session Management for the beginner

Jim Gallacher jpg at jgassociates.ca
Wed Jan 4 18:58:43 EST 2006

Jim Steil wrote:
> Jim:
> Thanks for the info.  However, I'm still having trouble.  I don't know 
> if this is the problem, but this code does NOT exist in a handler that I 
> wrote.  I didn't write a handler, I'm just using the publisher handler.  
> I have written an authentication handler and that is working correctly 
> but I don't know what I need to do to make my own handler and have it 
> work like the publisher handler.  Again, I'm probably trying too hard 
> and not seeing the obvious.  I have been looking all over the web and 
> can't seem to find a good custom handler example.
> Thanks again, I'll keep plugging away to try to understand what's going on.
>    -Jim

I tend to use the term "handler" to refer to anything that handles a 
request, which is just plain wrong. ;) If you want to use the publisher 
handler just drop a script in the appropriate directory and either call 
it (which will used the index method by default) or one of it's other 

Plop the following in your motion folder and try calling it with 
http://example.com/motion/mptest or 
http://example.com/motion/mptest/secUserList. (Note that the indentation 
may have been messed up when I did the copy and paste from your original 
code). Also note that I forgot the req parameter in my previous Session 
example. :(

You can modify your secUserList.html to make use of the count paramater 
passed to your psp template and see the effect of the session.



# uses the mod_python.publisher handler

import time
import mx.ODBC
import mx.ODBC.Windows

from mod_python import Session

# url is http://example.com/motion/mptest
def index(req):
     sess = Session.Session(req)
     if sess.is_new():
         sess['count'] = 0
     sess['count'] += 1
     req.content_type = 'text/plain'
     req.write('This is the index page\nVisits: %d\n' % sess['count'])

# url is http://example.com/motion/mptest/secUserList
def secUserList(req, page='', sortSeq=''):
     sess = Session.Session(req)
     if sess.is_new():
         sess['count'] = 0
     sess['count'] += 1

     if page:
         startPage = int(page)
         startPage = 1
     pageLength = 20
     sql = 'call secUser_list(\'' + sortSeq + '\', ' + repr(startPage) + 
', ' + repr(pageLength) + ')'
     db = 
     c = db.cursor()
     htmlText = ''
     for secUserId, firstName, lastName, logonName, email in c.fetchall():
         htmlText = htmlText + '<tr 
onmouseover="style.backgroundColor=\'#FFF0AF\'; style.cursor=\'hand\';" 
onclick="location.href=\'secUserProfile?secUserId=' + "%i" % secUserId + 
'\'" onmouseout="style.backgroundColor=\'#ffffff\';"><td>' + firstName + 
'</td><td>' + lastName + '</td><td>' + logonName + '</td><td>' + email + 
         previousPage = 1
     if startPage > 1:
         previousPage = startPage - 1
         nextPage = startPage + 1
     parmDict = {'count':sess['count'], 'title':'User List', 
'rowHtml':htmlText, 'previousPage':previousPage, 'nextPage':nextPage, 
     template = psp.PSP(req, filename='secUserList.html')
     template.run(vars = parmDict)

More information about the Mod_python mailing list