2014-09-28 30 views
38

我在運行Flask(一個Python模塊)中得到0會話。未在燒瓶會話中設置的密鑰

現在我用的燒瓶第三方庫Flask-Session

當我連接到我的網站,我收到以下錯誤:

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

下面是我的服務器代碼。

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

異常的*完整回溯*是什麼? – 2014-09-28 01:58:56

+0

你使用什麼版本的Flask-Session?我在[當前項目源代碼](https://github.com/fengsp/flask-session)中找不到該異常的任何引用。 – 2014-09-28 02:00:09

+0

@MartijnPieters任何想法如何我可以弄清楚?我只是做了一個點子安裝 – MintyAnt 2014-09-28 02:03:33

回答

41

唯一的例外是由NullSessionInterface會話實現,這是默認會話類型當您使用瓶上調-session。這是因爲你實際上沒有給配置SESSION_TYPE Flask;這是不夠將其設置爲您的模塊中的全局。

這個默認值對Flask 0.10沒有多大意義;它可能對Flask 0.8或0.9有意義,但當前版本被用作錯誤信號。在你的情況下,它現在給你錯誤的錯誤信息。

SESSION_TYPE配置選項設置爲其他值。選擇redis,memcached,filesystemmongodb之一。

將其設置爲filesystem是最簡單的;有足夠的默認配置那裏有沒有額外的依賴項工作:

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

    sess.init_app(app) 

    app.debug = True 
    app.run() 
+1

當我重命名SESSION_TYPE時,我仍然沒有多少運氣......也許這個庫已經過時了?你是否推薦了一個不同的瓶子會話方法?我可以嘗試一些燒瓶網站上列出的片段,但我也在那裏一頭扎進了一堵磚牆。我可以爲這些人發佈一個單獨的問題。 – MintyAnt 2014-09-28 02:12:48

+0

@MintyAnt:不確定您的意思*重命名SESSION_TYPE *;我爲你添加了一個示例配置。 – 2014-09-28 02:21:21

+1

@MintyAnt:啊,你把它定義爲一個全球性的,這還不夠。您需要將其設置爲'app'上的配置。 – 2014-09-28 02:24:45

6

試試這個:

app = Flask(__name__) 
app.config['SESSION_TYPE'] = 'memcached' 
app.config['SECRET_KEY'] = 'super secret key' 
sess = Session() 

,並在底部刪除您app.secret_key分配。

+0

我給了這個鏡頭,沒有運氣,同樣的錯誤。我可以更新帖子代碼,如果你願意 – MintyAnt 2014-09-28 02:17:38

28

設置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來運行你的應用,而python app.py__name__ != '__main__'

+0

我正在amazon ec2 apache2 Ubuntu服務器上運行燒瓶應用程序,使用oauth2.0訪問谷歌日曆信息。這個答案是使它工作的簡單修改。謝謝! – jas 2016-02-16 05:17:00

+0

謝謝!這解決了我的問題,而接受的答案沒有 – rlsw 2017-07-21 12:29:32

+0

爲什麼這解決了這個問題?這似乎很奇怪... – Garrett 2017-12-17 05:35:40