1.10.2 Example

The session.start() method returns a session object for the storage driver you chose. The session object can be treated like a dictionary to set and get variables. Any python object whcih can be pickled using the pickle module can be set in the session store.

Have a look at the example application below:

#!/usr/bin/env python

# show python where the web modules are
import sys
sys.path.append('../')
sys.path.append('../../../') 

import web, web.session, os

session = web.session.start(
    storage='file',
    app='test', 
    dir='../doc/src/lib/example-web-session', 
    expire=10,
)

print "Content-type: text/html\n\n"

if session.created:
    print """<html>
                <h1>This is your first visit</h1>
                <p>Setting variable1 to 'Python Rules!'.</p>
                <p>Refresh this page...</p>
            </html>"""
    session['variable1'] = 'Python Rules!'
else:
    if web.cgi.has_key('destroy'):
        session.destroy()
        print """<html><h1>Session Deleted</h1>
                <p><a href="%s">Start again</a>.
                </p></html>"""%(os.environ['SCRIPT_NAME'])
    else:
        print """<html><h1>Welcome back</h1><p>variable1: %s</p>
                <p><a href="%s?destroy=True">Destroy session</a>.
                </p></html>"""%(session['variable1'],os.environ['SCRIPT_NAME'])

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

See About this document... for information on suggesting changes.