现在我正在使用烧瓶第三方库Flask-Session,我没有运气会议。
当我连接到我的网站时,出现以下错误:
RuntimeError:会话不可用,因为没有密钥 组。将应用程序上的secret_key设置为唯一的 秘密。
以下是我的服务器代码。
from flask import Flask, session
from flask.ext.session import Session
SESSION_TYPE = 'memcache'
app = Flask(__name__)
sess = Session()
nextId = 0
def verifySessionId():
global nextId
if not 'userId' in session:
session['userId'] = nextId
nextId += 1
sessionId = session['userId']
print ("set userid[" + str(session['userId']) + "]")
else:
print ("using already set userid[" + str(session['userId']) + "]")
sessionId = session.get('userId', None)
return sessionId
@app.route("/")
def hello():
userId = verifySessionId()
print("User id[" + str(userId) + "]")
return str(userId)
if __name__ == "__main__":
app.secret_key = 'super secret key'
sess.init_app(app)
app.debug = True
app.run()
如您所见,我确实设置了应用密钥。我做错了什么?
是否有其他会话选项?
其他信息: 在Linux Mint上运行Python 2.7
完全粘贴:
Traceback (most recent call last):
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/sean/code/misc/session/sessiontest.py", line 27, in hello
userId = verifySessionId()
File "/home/sean/code/misc/session/sessiontest.py", line 16, in verifySessionId
session['userId'] = nextId
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/werkzeug/local.py", line 341, in __setitem__
self._get_current_object()[key] = value
File "/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/sessions.py", line 126, in _fail
raise RuntimeError('the session is unavailable because no secret '
RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
答案 0 :(得分:64)
在您的情况下,NullSessionInterface
会话实现引发了异常,当您使用Flask-Session时,该实现是默认会话类型。这是因为您实际上并未将SESSION_TYPE
配置提供给Flask ; 还不够将其设置为模块中的全局。 Flask-Session quickstart example code确实设置了全局,但是通过调用app.config.from_object(__name__)
将当前模块用作配置对象。
对于Flask 0.10或更新版本,此默认值没有多大意义; NullSession
可能对Flask 0.8或0.9有意义,但在当前版本中,flask.session.NullSession
class用作错误信号。在你的情况下,它现在给你错误的错误信息。
将SESSION_TYPE
配置选项设置为其他选项。选择redis
,memcached
,filesystem
或mongodb
中的一个,并确保在app.config
中设置(直接或通过various Config.from_*
methods)
要进行快速测试,将其设置为filesystem
是最简单的;有足够的默认配置可以在没有其他依赖项的情况下完成工作:
if __name__ == "__main__":
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
sess.init_app(app)
app.debug = True
app.run()
如果你看到这个错误并且不使用Flask-Session,那么设置秘密会出现问题。如果您在上面的app.config['SECRET_KEY']
警卫中设置app.secret_key
或if __name__ == "__main__":
并且您收到此错误,那么您可能正在通过导入Flask项目的WSGI服务器运行Flask应用程序作为模块,__name__ == "__main__"
块永远不会运行。
无论如何,manage configuration for Flask apps in a separate file总是更好。
答案 1 :(得分:44)
将密钥设置在if __name__ == '__main__':
app.py:
from flask import Flask, session
app = Flask(__name__)
app.secret_key = "super secret key"
@app.route("/")
...
if __name__ == '__main__':
app.debug = True
app.run()
当您通过运行flask run
启动应用时,会跳过if __name__ == '__main__':
块。如果您不想跳过它,请使用python app.py
。
答案 2 :(得分:9)
试试这个:
app = Flask(__name__)
app.config['SESSION_TYPE'] = 'memcached'
app.config['SECRET_KEY'] = 'super secret key'
sess = Session()
并删除底部的app.secret_key
作业。