1.11.11 Example

Here is a full example showing the creation of all the necessary objects and giving you full control over the session:

#!/usr/bin/env python

# show python where the modules are
import sys; sys.path.append('../'); sys.path.append('../../../') 
import web.error; web.error.enable()
import os, time
import web.database

# Setup a database connection
connection = web.database.connect(
    adapter="snakesql", 
    database="webserver-session",
    autoCreate = 1,
)
cursor = connection.cursor()

# Obtain a session manager the full way
import web.session
manager = web.session.manager(driver='database', cursor=cursor, autoCreate=1)
sessionID = manager.cookieSessionID()
if not manager.load(sessionID):
    manager.create(sendCookieHeaders=False)
    manager.sendCookieHeaders()
store = manager.store('testApp')

def printPage(title, url, link, url2, link2, data):
    print """
    <html>
    <h1>%s</h1>
    <p><a href="%s">%s</a></p>
    <p><a href="%s">%s</a></p>
    <p>%s</p>
    </html>"""%(title, url, link, url2, link2, data)
                
# Write a simple application the full way
if not manager.created:
    if web.cgi.has_key('destroy') and web.cgi['destroy'].value == 'True':
        manager.destroy(ignoreWarning=True, sendCookieHeaders=False) 
        manager.sendCookieHeaders()
        print web.header('text/html')
        printPage(
            'Session Destroyed', 
            os.environ['SCRIPT_NAME'], 
            'Start Again', '','',''
        )
    else:
        manager.setExpire(manager.expireTime+5, sendCookieHeaders=1)
        print web.header('text/html')
        data = []
        data.append('SessionID:  ' +manager.sessionID)
        data.append('Store Keys: '+str(store.keys()))
        data.append('Store App:  '+store.app)
        data.append('Variable1:  '+str(store['Variable1']))
        data.append('ExpireTime: '+str(manager.expireTime))
        printPage(
            'Welcome back', 
            os.environ['SCRIPT_NAME'], 
            'Visit Again', 
            os.environ['SCRIPT_NAME']+'?destroy=True', 
            'Destroy Session', 
            '<p>Every time you visit this page the expiry \
            time increases 5 seconds</p>'+'</p><p>'.join(data)
        )
else:
    print web.header('text/html')
    store['Variable1'] = 'Python Rules!'
    printPage(
        'New Session Started',
        os.environ['SCRIPT_NAME'], 
        'Visit Again', '', '',
        "Set variable1 to 'Python Rules!'"
    )

connection.commit() # Save changes
connection.close()  # Close the database connection

You can test this example by running the webserver scripts/webserver.py and visiting http://localhost:8080/doc/src/lib/webserver-web-session.py