Webpy Sessions with WSGI

I was having trouble getting sessions to work using webpy 0.34 and apache/WSGI and realized late last night what the issue was.

I created a basic app and added the obligatory wsgi code -

if __name__ == "__main__":app.run() #code necessary for wsgi here app = web.application(urls, globals(), autoreload=False) application = app.wsgifunc()

Then without thinking about it I went and looked over the examples of session handling in the webpy docs and added the code to create the session store as was shown in the examples. This code was added to the head of the file just after the imports –

store = web.session.DiskStore('sessions') app = web.application(urls, globals()) session = web.session.Session(app, store)

This looked good but no luck. Tried disk store and a db store for the sessions and nothing. Well after a couple of hours of staring at the code I realized I was declaring “app = …” twice in the code. Once to setup the sessions and then again to setup wsgi. So to fix I moved the code to handle the wsgi setup to run before the session setup code.

So now my session and wsgi setup code looks like this. This code is just after the definition of the URLs -

urls=('/', 'index') #code necessary for wsgi here app = web.application(urls, globals(), autoreload=False) application = app.wsgifunc() #code for sessions here store = web.session.DiskStore('sessions') session = web.session.Session(app, store)

So far this seems to work.

Leave a comment