未在Flask会话中使用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.

回答:

在你的情况下,NullSessionInterface会话实现会引发异常,这是使用Flask-Session时的默认会话类型。这是因为你从未真正将SESSION_TYPE配置提供给Flask;仅在模块中将其设置为全局变量是不够的。所述瓶会话快速启动的示例代码并设置一个全局,但随后通过调用使用当前模块作为配置对象app.config.from_object(__name__)

对于Flask 0.10或更高版本,此默认设置没有太大意义;NullSessionFlask 0.8或0.9可能是有意义的,但是在当前版本中,flask.session.NullSession该类用作错误信号。就你而言,它现在给你错误的错误消息。

将SESSION_TYPE配置选项设置为其他内容。挑一个redis,memcached,filesystemmongodb,并确保其设置在app.config(直接或通过各种Config.from_*方法)。

为了进行快速测试,将其设置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__"块永远不会运行。

以上是 未在Flask会话中使用Flask-Session扩展名设置密钥 的全部内容, 来源链接: utcz.com/qa/427539.html

回到顶部