1.14.3.5 web.wsgi.session - Session Handling

The web.wsgi.session module provides one class Session

class Session( application, [**params])

application
A WSGI application or middleware component

**params
Any parameters which can be passed to web.session.manager(). If cursor is not specified it is assumed that you are using Database middleware and the cursor is obtained from environ['web.database.cursor']

Entries added to environ:

environ['web.session']
A SessionManager object as returned by web.session.manager(). You can obtain a session store using store = self.environ['web.session'].store('testApp') replacing 'testApp' with the name of your application's store.

The web.wsgi.session.Session middleware requires the presence of the Database middleware and can be used as shown in the example below:

from web.wsgi import *

class simpleApp(base.BaseApplication):

    def printPage(self, title, url, link, url2, link2, data):
        self.output("""
            <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)
        )
    def start(self):
        # Write a simple application
        store = self.environ['web.session'].store('testApp')
        
        if not self.environ['web.session'].created:
            if self.environ['web.cgi'].has_key('destroy') and self.environ['web.cgi']['destroy'].value == 'True':
                self.environ['web.session'].destroy(ignoreWarning=True, sendCookieHeaders=False) 
                self.headers.append(self.environ['web.session'].response_headers[-1])
                self.printPage(
                    'Session Destroyed', 
                    self.environ['SCRIPT_NAME'], 
                    'Start Again', '','',''
                )
            else:
                self.environ['web.session'].setExpire(self.environ['web.session'].expireTime+5)
                self.environ['web.session'].setCookie()
                self.headers.append(self.environ['web.session'].response_headers[-1])
                data = []
                data.append('SessionID:  '+self.environ['web.session'].sessionID)
                data.append('Store Keys: '+str(store.keys()))
                data.append('Store App:  '+store.app)
                data.append('Variable1:  '+str(store['Variable1']))
                data.append('ExpireTime: '+str(self.environ['web.session'].expireTime))
                self.printPage(
                    'Welcome back', 
                    self.environ['SCRIPT_NAME'], 
                    'Visit Again', 
                    self.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:
            store['Variable1'] = 'Python Rules!'
            self.printPage(
                'New Session Started',
                self.environ['SCRIPT_NAME'], 
                'Visit Again', '', '',
                "Set variable1 to 'Python Rules!'"
            )
        # Save changes
        self.environ['web.database.connection'].commit()

application = error.Error(
    database.Database(
        session.Session(
            cgi.CGI(
                simpleApp(),
            ),
            expire = 10,
            autoCreate = 1,
            driver = 'database',
        ),
        adapter = 'snakesql',
        database = 'wsgi-session',
        autoCreate = 1
    ),
)

You can test this example by running the WSGI server scripts/WSGIServer.py and visiting http://localhost:8000/session