Jim Gallacher
jpg at jgassociates.ca
Wed Jan 4 12:24:57 EST 2006
Jim Steil wrote: > Hi, I'm new to mod_python and apache in general, and am confused about > how to implement session management. I'm using the Publisher handler > and PSP templates. > > Here is the relevant portion of my http.conf file: > > <Directory "c:/program files/apache group/apache2/htdocs/motion"> > AddHandler mod_python .py > PythonHandler mod_python.publisher > PythonAuthenHandler myAuthScript > PythonDebug On > AuthType Basic > AuthName "Restricted Area" > require valid-user > </Directory> > > ...and here is my python module: > > from mod_python import psp > > import time > import mx.ODBC > import mx.ODBC.Windows > > def secUserList(req, page='', sortSeq=''): > if page: > startPage = int(page) > else: > startPage = 1 > pageLength = 20 > sql = 'call secUser_list(\'' + sortSeq + '\', ' + repr(startPage) + > ', ' + repr(pageLength) + ')' > db = mx.ODBC.Windows.DriverConnect('DSN=motion;UID=userid;PWD=password') > c = db.cursor() > c.execute(sql) > 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 + > '</td></tr>\n' > previousPage = 1 > if startPage > 1: > previousPage = startPage - 1 > nextPage = startPage + 1 > c.close() > db.close() > parmDict = {'title':'User List', 'rowHtml':htmlText, > 'previousPage':previousPage, 'nextPage':nextPage, 'startPage':startPage} > template = psp.PSP(req, filename='secUserList.html') > template.run(vars = parmDict) > > > My question is; How do I implement the session? from mod_python import Session Where do I put the > code? Most likely in your handler, (secUserList?) but it can go where ever you want to access the session data. Make sure you only create one session object per request though. If you need to pass it around to other functions in a single request then stuff it into your request object. def secUserList(req): # creating the session instance will either load an existing session # or create a new one req.sess = Session.Session() if req.sess.is_new(): req.sess['visits'] = 0 req.write('you have visited this page %d times' % req.sess['visits']) doSomeOtherStuff(req) req.sess['visits'] += 1 # You must save the session to make it persistent req.sess.save() def doSomeOtherStuff(req): req.sess['stuff'] = 'wonderful' > I want to be able to store my database login credentials in the > session object. I'm guessing I'm missing something really obvious, but > can't seem to get my head around it. Don't think so hard. ;) It really is as simple as it looks. > Any help or pointers to good > examples would really be appreciated. Just think of the session object as a persistent dictionary. The correct dictionary to load for a request is determined by a cookie which is automatically set for you when you create the session. Jim
|