gpt4 book ai didi

python - Flask session 不持久

转载 作者:太空狗 更新时间:2023-10-29 21:14:46 25 4
gpt4 key购买 nike

我在 CentOS 6.3 上使用 Python 2.7、Apache + mod_wsgi 运行

当我在本地主机上时一切正常。但是,当我在 Azure 中的虚拟机上运行代码时,我没有看到跨页面持久保存 session 信息。

基本上在我看来,我有这样的东西:

@frontend.route('/')
def index():
session['foo'] = 'bar'
print session['foo']

return redirect(url_for("frontend.page2"))

@frontend.route('page2')
def page2():
print session

打印输出为:

bar
<SecureCookieSession {}>

我的 apache wsgi 配置是:

WSGISocketPrefix /var/run/wsgi

<VirtualHost *:80>
ServerName example.com
ServerAlias example.com

WSGIDaemonProcess myproj threads=5 processes=5
WSGIScriptAlias / /home/mydir/myproj/apache/myproj.wsgi

<Directory /home/mydir/myproj>
WSGIScriptReloading On
WSGIProcessGroup myproj
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

我设置了 secret_key:

app.secret_key = os.urandom(24)

我已尝试同时设置 SERVER_NAME,但没有帮助:

app.config['SERVER_NAME'] = 'example.com' 

关于如何进一步调试它的任何想法?

谢谢!

最佳答案

不要使用 app.secret_key = os.urandom(24)!

你应该在这里输入一个静态值,而不是每次都从 os.urandom 中读取。您可能误解了 docs 中的示例,它向您展示了如何从 os.urandom 中读取随机数据,但它也明确指出:

Just take that thing and copy/paste it into your code and you’re done

如果你在运行时读取它,那么你的每个工作进程都会有一个不同的 key !这意味着如果请求由不同的工作人员处理, session 将中断,因为 cookie 使用错误的 key 签名。

关于python - Flask session 不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18709213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com