【问题标题】:python bottle persistent cookie not workingpython瓶持久cookie不起作用
【发布时间】:2011-10-28 06:44:59
【问题描述】:

我正在开发一个网站,我想在 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) 

【问题讨论】:

    标签: python cookies bottle


    【解决方案1】:

    Bottle 使用http://docs.python.org/library/cookie.html 来实现cookie 支持。此实现要求expires 参数是Wdy, DD-Mon-YY HH:MM:SS GMT 格式的字符串。传递 datetime 或 date 对象会静默失败。

    我会在以后的 Bottle 版本中解决这个问题(嗨,我是作者),但现在我建议改用 max_age

    编辑:哦,我刚刚注意到它也被错误地记录了。对此感到抱歉。 Edit2:已修复(在母版中)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2012-07-01
    • 2013-09-13
    相关资源
    最近更新 更多