私のコードの何が問題になっていますか? WSGI、ビーカー、Python、apache2

StackOverflow https://stackoverflow.com/questions/5847933

質問

私は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に行くときに正常に機能します。 2つの変更を試みると、サイトは失敗します。

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

これはクッキーエラーです:

[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.
役に立ちましたか?

解決

のために session.save() エラーは、設定して保存する場所を教えていないと書かれています data_dir セッションキャッシュパラメーター

Cookieエラーには、秘密の鍵が必要です validate_key ブラウザに送信されたときにCookieをエンコードし、ブラウザからCookieを取得したときにデコードおよび認証できるように設定します

したがって、それらを解決するために、これらのキーを独自の値で追加します。

ファイルの場合:

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

クッキー用:

session_opts['validate_key'] = "some secret value"
session_opts['encrypt_key'] = "some other value"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top