Flask会话不会持续 [英] Flask session not persisting
问题描述
当我在本地主机上时,情况正常。但是,当我在Azure的虚拟机上运行代码时,我没有看到会话信息在页面中持续存在。
基本上,在我看来,我有这样的:
$ b $
@ frontend.route('/')
def index():
session ['foo '] ='bar'
print session ['foo']
return redirect(url_for(frontend.page2))
@frontend.route 'page2')
def page2():
打印会话
输出是:
bar
< SecureCookieSession {}>
我的wsgi配置为apache:
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}
排序拒绝,允许
允许所有
< / 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
每次。您可能误解了文档中的示例,它会告诉您如何从 os.urandom
,但也明确指出:
将它粘贴到你的代码中,你就完成了。
如果你在运行时读取它,那么你的每个工作进程将会有一个不同的密钥!这意味着如果一个请求是由不同的工作人员处理的,会话将会中断,因为Cookie使用错误的密钥进行签名。Am running with Python 2.7, Apache + mod_wsgi on CentOS 6.3
Things work fine when I am on localhost. However, when I run the code on a vm in Azure, I do not see the session information being persisted across pages.
Basically in my views, I have something like:
@frontend.route('/') def index(): session['foo'] = 'bar' print session['foo'] return redirect(url_for("frontend.page2")) @frontend.route('page2') def page2(): print session
The print output is:
bar <SecureCookieSession {}>
My wsgi configuration for apache is:
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>
I have the secret_key set:
app.secret_key = os.urandom(24)
I have tried with both setting SERVER_NAME but it doesn't help:
app.config['SERVER_NAME'] = 'example.com'
Any ideas on how I can debug this more?
Thanks!
解决方案Don't use
app.secret_key = os.urandom(24)
!You're supposed to enter a static value here, not read from
os.urandom
each time. You've probably misunderstood the example in the docs, it shows you how you can read random data fromos.urandom
, but it also clearly states:Just take that thing and copy/paste it into your code and you’re done
If you read it at runtime, then each of your worker processes will have a different secret key! That means if a request is handled by a different worker, the session will break because the cookie is signed with the wrong secret key.
这篇关于Flask会话不会持续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!