我的代码出了什么问题? WSGI,烧杯,python,apache2

时间:2011-05-01 10:57:08

标签: python apache mod-wsgi wsgi beaker

我正在使用apache2,python26,或多或少的最新版烧杯,mod_wsgi

这是我的代码,我是从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)

当我转到localhost / beaker2.wsgi时这很好用。当我尝试两次更改时,网站失败:

1。 添加

session.save()

2。 变化

'session.type': 'cookie',

为什么在添加session.save()或将session.type更改为cookie时会出错?

完全引用: 抱歉的日期和一切,用尾巴-f

做到了

这是针对session.save()错误:

[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

这是cookie错误:

[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.

1 个答案:

答案 0 :(得分:3)

对于session.save()错误,它表示您没有通过设置data_dir会话缓存参数

告诉它在哪里保存

对于cookie错误,它需要设置一个密钥validate_key,以便在发送到浏览器时对cookie进行编码,并在从浏览器获取cookie时对其进行解码和验证

所以要解决这些问题,请使用您自己的值添加这些键:

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"