Home

  Documentation

  Download

  News

  Examples

session.py Source Code

C:/Work/Lemon/Backups/Lemon-0.2.10/Lemon-0.2.10/../../doc/examples/code/session.py
#!/usr/bin/env python

"Main session example."

import cgitb; cgitb.enable() # Enable the error reporting engine so that you can see any errors.
import sys, re
sys.path.append('../../../')
sys.path.append('../')

import lemon.session, lemon, lemon.db

# Set up a lemon.db cursor to a database with a lemon session table and create the session object.

#print "Content-type: text/html

"
session = lemon.session.start(
    expires=10, cleanup=1,
    storage='db',
    database=lemon.db.connect(
        type='gadfly', 
        database='Lemon',
        dir='../../../doc/examples/data/gadfly',
    ),
    app='test',
    #database=lemon.db.connect(type='mysql', database='test')
)

if session.created():
    print "Content-type: text/html

"
    print "<html><h1>New Session</h1><p>Setting variable1 to 'Python Rules!'.</p><p><a href=\"session.py\">Visit this page again</a>.</p><p>The session will expire after 10 seconds from creation.</p></html>"
    # Demonstrate the types of data that can be stored    
    session.set('variable1','Python Rules!')
else:
    if lemon.get.has_key('destroy'):
        #print "Content-type: text/html

"
        session.destroy()
        print "Content-type: text/html

"
        print "<html><h1>Session Deleted</h1><p><a href=\"session.py\">Visit this page again</a>.</p></html>"
    else:
        print "Content-type: text/html

"
        print "<html><h1>Welcome Back</h1><p>variable1: "
        # Demonstrate retrieving data
        print session.get('variable1'),
        print '</p><p><a href="session.py?destroy=True">Destroy session</a>. <a href="session.py">Visit this page again</a>.</p></html>'

© 2002-2003 James Gardner