3

I'm using apache2, python26, more or less latest version of beaker, mod_wsgi

Here's my code, I got this from http://beaker.groovie.org/sessions.html#using

from beaker.middleware import SessionMiddleware

def simple_app(environ, start_response):
    # Get the session object from the environ
    session = environ['beaker.session']

    # Check to see if a value is in the session
    if 'logged_in' in session:
        user = True
    else:
        user = False

    # Set some other session variable
    session['user_id'] = 10

    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['User is logged in: %s' % user]

# Configure the SessionMiddleware
session_opts = {
    'session.type': 'file',
    'session.cookie_expires': True,
}
wsgi_app = SessionMiddleware(simple_app, session_opts)

This works fine when I go to localhost/beaker2.wsgi. When I try two changes the site fails:

1. add

session.save()

or

2. change

'session.type': 'cookie',

Why do I get errors when I add session.save() or change session.type to cookie?

full trackback: sorry for the dates and everything, did it with tail -f

This is for the session.save() error:

[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1] mod_wsgi (pid=25311): Exception occurred processing WSGI script '/home/yizi/apache2/www/beaker2.wsgi'.
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/middleware.py", line 152, in __call__
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     return self.wrap_app(environ, session_start_response)
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/home/yizi/apache2/www/beaker2.wsgi", line 21, in simple_app
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     start_response('200 OK', [('Content-type', 'text/html')])
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/middleware.py", line 146, in session_start_response
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     session.persist()
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/session.py", line 609, in persist
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     self._session().save()
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/session.py", line 275, in save
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     **self.namespace_args)
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/container.py", line 518, in __init__
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1]     raise MissingCacheParameter("data_dir or file_dir is required")
[Sun May 01 04:35:55 2011] [error] [client 127.0.0.1] MissingCacheParameter: data_dir or file_dir is required

This is the cookie error:

[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1] mod_wsgi (pid=25312): Exception occurred processing WSGI script '/home/yizi/apache2/www/beaker2.wsgi'.
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/middleware.py", line 152, in __call__
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]     return self.wrap_app(environ, session_start_response)
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]   File "/home/yizi/apache2/www/beaker2.wsgi", line 10, in simple_app
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]     if 'logged_in' in session:
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/session.py", line 579, in __contains__
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]     return self._session().has_key(key)
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/session.py", line 547, in _session
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]     self.__dict__['_sess'] = CookieSession(req, **params)
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.6/dist-packages/Beaker-1.5.4-py2.6.egg/beaker/session.py", line 373, in __init__
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1]     raise BeakerException("No validate_key specified for Cookie only "
[Sun May 01 04:53:18 2011] [error] [client 127.0.0.1] BeakerException: No validate_key specified for Cookie only Session.
Derek
  • 151
  • 1
  • 5

1 Answers1

3

for the session.save() error it says that you haven't told it where to save by setting the data_dir session cache parameter

for the cookie error, it needs a secret key validate_key set so that the cookies can be encoded when sent to the browser and decoded and authenticated when it gets the cookies from the browser

so to solve them add these keys with your own values:

for file:

session_opts['data_dir'] = "/some/place/to/save/"

for cookies:

session_opts['validate_key'] = "some secret value"
session_opts['encrypt_key'] = "some other value"
Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • The data_dir tip worked, although, I had to chmod the folder where it was placed to grant permissions, is there a way in the wsgi or on apache to grant permissions? Also the cookies thing didn't work. I double declared the dictionary to be sure, but the error is the same. session_opts = { 'session.type': 'cookie', 'session.cookie_expires': True, 'validate_key' : 'key', 'encrypt_key' : 'secret', } session_opts['validate_key'] = "key" session_opts['encrypt_key'] = "secret" – Derek May 01 '11 at 21:13
  • Also, since you know a lot about wsgi and beaker, could you please also look at my other question at http://stackoverflow.com/questions/5846404/whats-wrong-with-my-beaker-wsgi-apache2-python, thanks for all the help so far! – Derek May 01 '11 at 21:15
  • Got the cookies to work like you said, don't know why it didn't work the first time. Thanks! Hey would you know how to connect beaker to mysql? – Derek May 01 '11 at 21:23
  • Nvm, got mysql to work with beaker. However, again, since it needed a lock_dir I need to chmod 777 a directory on my ubuntu. Is there a way around this or doing it more effectively through wsgi or apache? – Derek May 01 '11 at 21:34