[mod_python] Session Management for the beginner

Jim Steil jim at qlf.com
Wed Jan 4 18:07:23 EST 2006


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

Jim Steil
IT Manager
Quality Liquid Feeds
(608) 935-2345


Jim Gallacher wrote:
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060104/7023d287/attachment.html


More information about the Mod_python mailing list