秘密密钥没有设置在烧瓶会议 [英] secret key not set in flask session

查看:208
本文介绍了秘密密钥没有设置在烧瓶会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我正在使用第三方库a a href =, http://pythonhosted.org/Flask-Session/\">Flask-Session



当我连接到我的网站时,出现以下错误:


RuntimeError:会话不可用,因为没有密钥是
set。设置应用程序的secret_key为独特的
秘密。

以下是我的服务器代码。

 从flask导入烧瓶,会话$ b $ from flask.ext.session import Session 

SESSION_TYPE ='memcache'
$ b app = Flask(__ name__)
sess = Session()
$ b $ nextId = 0

def verifySessionId():
global nextId
$ b $如果session中没有'userId':
session ['userId'] = nextId
nextId + = 1
sessionId = session ['userId' ]
print(set userid [+ str(session ['userId'])+])
else:
print(using already set userid [+ str ['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 ='超级密钥'

sess.init_app(app)

app.debug = True
app.run()

正如您所看到的,我设置了应用程序密钥。还有其他的会话选项吗?

其他信息:
正在运行在Python Mint上的Python 2.7



完整粘贴:

  Traceback最近调用最后一个):
在__call__
文件中/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py,第1836行返回self.wsgi_app(environ,start_response)
在wsgi_app中的文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py,第1820行
response = self.make_response(self.handle_exception(e))
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app。 py,行1403,在handle_exception
reraise(exc_type,exc_value,tb)
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask /app.py,第1817行,在wsgi_app
response = self.full_dispatch_request()
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages /flask/app.py行1477,在full_dispatch_request
中rv = self.handle_user_exception(e)
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/app.py ,第1381行,在handle_user_exception
reraise(exc_type,exc_value,tb)
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/ app.py,第1475行,在full_dispatch_request
rv = self.dispatch_request()
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/ / home / sean / code / misc / session / sessiontest,这个文件就是一个文本文件,其中包含了一个名为flask / app.py的行1461,在dispatch_request
中返回self.view_functions [rule.endpoint](** req.view_args)
.py第27行,在hello
userId = verifySessionId()
文件/home/sean/code/misc/session/sessiontest.py,第16行,在verifySessionId
session ['userId'] = nextId
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/werkzeug/local.py,行341,在__setitem__
self._get_current_object()[key] = v alue
文件/home/sean/code/misc/hangman/venv/lib/python2.7/site-packages/flask/sessions.py,行126,in _fail
raise RuntimeError('会话不可用,因为没有秘密
RuntimeError:会话不可用,因为没有设置密钥。将应用程序上的secret_key设置为独特和秘密的内容。


解决方案

异常是由 NullSessionInterface 会话实现,当您使用Flask-Session时,这是默认会话类型。这是因为您实际上没有给Flask 配置 SESSION_TYPE 配置;它是不够的将其设置为您的模块中的全球。



这个默认与Flask 0.10没有太大关系;它可能对Flask 0.8或0.9有意义,但当前版本被用作错误信号。在你的情况下,它现在给你错误的信息。



设置 SESSION_TYPE 配置选项。选择一个 redis memcached 文件系统 mongodb



设置为 filesystem 有足够的默认配置有没有额外的依赖性的工作:

pre $如果__name__ ==__main__:
app.secret_key ='超级密钥'
app.config ['SESSION_TYPE'] ='文件系统'

sess.init_app(app)

app.debug = True
app.run()


I am having 0 luck getting a session working in Flask (a Python module).

Right now I am using a flask 3rd party library Flask-Session

When I connect to my site, I get the following error:

RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

Below is my server code.

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()

As you can see, I do set the app secret key. What am I doing wrong?

Are there other session options?

Other info: Running Python 2.7 on Linux Mint

Full paste:

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.

解决方案

The exception is raised by the NullSessionInterface session implementation, which is the default session type when you use Flask-Session. That's because you don't ever actually give the SESSION_TYPE configuration to Flask; it is not enough to set it as a global in your module.

This default doesn't make much sense with Flask 0.10; it may have made sense with Flask 0.8 or 0.9, but the current version is used as an error signal. In your case it gives you the wrong error message now.

Set the SESSION_TYPE configuration option to something else. Pick one of redis, memcached, filesystem or mongodb.

Setting it to filesystem is easiest; there is enough default configuration there to have that work without additional dependencies:

if __name__ == "__main__":
    app.secret_key = 'super secret key'
    app.config['SESSION_TYPE'] = 'filesystem'

    sess.init_app(app)

    app.debug = True
    app.run()

这篇关于秘密密钥没有设置在烧瓶会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆