樱桃皮'工具.会话.安全“似乎中断了会议

2024-10-01 00:32:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在localhost上开发一个cherrypy应用程序,并编写本文来了解sessions的情况。在

import cherrypy

class WhyNotSessions(object):

    @cherrypy.expose
    def index(self):
        if 'count' not in cherrypy.session:
            cherrypy.session['count'] = 0
        cherrypy.session['count'] += 1
        return "Session count is %s" % cherrypy.session.get('count')

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.sessions.secure': True
        }
    }
    cherrypy.quickstart(WhyNotSessions(), '/', conf)

这和预期的一样,只要我从conf中注释出'tools.sessions.secure': True,重载时count递增。我想更好地了解这里发生了什么,因为我打算在生产中使用安全会话。在


Tags: importtrue应用程序localhostifsessionconfcount
1条回答
网友
1楼 · 发布于 2024-10-01 00:32:29

我刚刚也遇到了同样的问题。在

这是因为工具.会话.安全'to True将“secure”标志添加到生成的存储会话id的cookie中

如果您没有在CherryPy中使用HTTPS,那么在以后的任何请求中都不会返回这个cookie,因此每次都会生成一个新的会话id。在

在CherryPy中启用HTTPS解决了这个问题。请参见CherryPy documentation on SSL了解如何打开此功能。在

相关问题 更多 >