Python Bottle持久化cookie无法工作

3
我正在处理一个网站,想要在cookie中存储一个值。这个值是一个数字,在用户访问网站时,我想知道上次访问时的数字是什么,所以我考虑使用持久化cookie来存储当前值。当用户访问网站时,如果没有会话cookie,则会话cookie会复制持久性cookie。这样,会话cookie始终具有上次访问的值。
然而,尽管我将过期日期设置为从现在开始的一年,但似乎我的持久性cookie并没有被保留下来。
以下是我的Python代码:
persistentCookieKey = category + '_highest_id'
sessionCookieKey = 'session_' + persistentCookieKey + '_highest_id'

persistentCookieValue = request.get_cookie(persistentCookieKey)
if persistentCookieValue == None:
    persistentCookieValue = 0      # each time i restart my browser it comes through here!

sessionCookieValue = request.get_cookie(sessionCookieKey)
print 'persistentCookieValue:', persistentCookieValue
print 'sessionCookieValue:', sessionCookieValue

if sessionCookieValue == None:
    print 'session cookie not set, setting to:', persistentCookieValue
    sessionCookieValue = persistentCookieValue
    response.set_cookie(sessionCookieKey, str(persistentCookieValue))

print 'setting persistent cookie to value:', highestId
expireDate = date.today() + timedelta(days=365)
response.set_cookie(persistentCookieKey, str(highestId), expires=expireDate)

highestIdLastVisit = int(sessionCookieValue) 
1个回答

9
瓶子使用http://docs.python.org/library/cookie.html来实现cookie支持。该实现需要将expires参数作为字符串以Wdy, DD-Mon-YY HH:MM:SS GMT格式提供。传递日期时间或日期对象会静默失败。
我会在未来的版本中修复这个问题(嗨,我是作者),但是目前我建议改用max_age代替。
编辑:哦,我刚刚发现文档也有错误。对此很抱歉。 编辑2:已经修复(在主代码库中)。

Cookies不正常工作。 我该如何解决?我使用的是Python格式。但无效。以下是我的帖子:https://dev59.com/pGbWa4cB1Zd3GeqPZrd9 - Tampa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接