烧瓶会议没有重新加载

时间:2014-09-28 02:49:40

标签: python session flask

现在我正在使用Flask和一个烧瓶第三方库Flask-Session

使用下面的代码,我重新加载页面4次并得到以下输出:

set userid[0]
127.0.0.1 - - [27/Sep/2014 22:28:35] "GET / HTTP/1.1" 200 -
set userid[1]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[2]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[3]
127.0.0.1 - - [27/Sep/2014 22:28:38] "GET / HTTP/1.1" 200 -

代码:

from flask import Flask, session
from flask.ext.session import Session

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()
    return str(userId)

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

    sess.init_app(app)

    app.debug = True
    app.run()

不应该将会话[' userId]保存在'每次我重新加载页面?

1 个答案:

答案 0 :(得分:2)

您需要启用Cookie才能使会话正常运行。即使没有这些浏览器,Flask-Session也无法跟踪浏览器。

Flask-Session设置一个具有唯一ID的cookie,然后再通过该cookie再次查找会话数据。