简体   繁体   English

Flask会话cookie未在Safari中设置

[英]Flask session cookie not set in Safari

I experienced a strange behavior with my session cookie: Running the flask app on my mac, everything works fine and on any browser the cookie is set. 我的会话cookie经历了一个奇怪的行为:在我的mac上运行烧瓶应用程序,一切正常,并且在任何浏览器上设置了cookie。

However, if I run it on a windows server, the session cookie is not set on Safari (and iOS) - but still works on any other browsers. 但是,如果我在Windows服务器上运行它,会话cookie不会在Safari(和iOS)上设置 - 但仍然适用于任何其他浏览器。 How can this happen? 怎么会发生这种情况? Here is an example of a simple app: 这是一个简单应用程序的示例:

import os
import uuid
from flask import Flask, render_template, session

app = Flask(__name__)

SESSION_LIFETIME = 3600

@app.before_request
def before_request():

    # create session
    if not session.get('uid'):
        session.permanent = True
        session['uid'] = uuid.uuid4()

@app.route('/', methods=['GET'])
def test():

    return render_template('test.html')

if __name__ == "__main__":
    app.secret_key = os.urandom(24)
    app.permanent_session_lifetime = SESSION_LIFETIME
    app.debug = True
    app.run(threaded=True,
            host="0.0.0.0",
            port=int("5000")
            )

with an example test.html: 使用示例test.html:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Wubwub</title>
</head>
<body>
Jojo
</body>
</html>

Why does it work on any browser but not on the (important) Safari? 为什么它适用于任何浏览器而不适用于(重要)Safari? And why does the same code work when run on my mac (accessed from both outside and local), but not on windows? 为什么在我的mac上运行相同的代码(从外部和本地访问),但在Windows上没有? All other browsers work with windows (even from outside). 所有其他浏览器都使用Windows(甚至从外部)。

I had same behaviour that session variables were not working as they want. 我有相同的行为,会话变量不能正常工作。

So what I did is removed the session use and to make work like as session I used a list with key-value pair 所以我所做的是删除会话使用,并使工作像会话我使用list with key-value pair

First init the list 首先初始化列表

 list_name = {'key1':'','key2':''};  and so on...

And store the variables as you wish in this list and access it wherever you want by substituting the keys 并根据需要将变量存储在此列表中,并通过替换键来访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM