【问题标题】:How to set cookies with GAE/Python for 1 month?如何使用 GAE/Python 设置 1 个月的 cookie?
【发布时间】:2016-01-23 05:37:17
【问题描述】:

我需要实现以下内容:

  1. 用户输入用户ID和密码
  2. 我们在另一台服务器上进行验证
  3. 如果正确,包含这些详细信息的 cookie 应保存一个月
  4. 每次用户使用我的网站时,我们都应该查找 cookie
  5. 如果没有找到 - 转到第 1 步

如何设置 1 个月的 cookie?

以下是否有效?

self.response.headers.add_header(
        'Set-Cookie', 
        'credentials=%s; expires=Fri, 31-Dec-2020 23:59:59 GMT' \
          % credentials.encode())

如何按要求的格式计算从现在开始的一个月?

【问题讨论】:

    标签: python google-app-engine datetime cookies


    【解决方案1】:

    你可以使用 webapp.Response.set_cookie() 方法:

    import datetime
    
    self.response.set_cookie('name', 'value', expires=datetime.datetime.now(), path='/', domain='example.com')
    

    Cookie 的日期格式如下所示:

    print (datetime.datetime.now() + datetime.timedelta(weeks=4)).strftime('%a, %d %b %Y %H:%M:%S GMT') 
    

    【讨论】:

    • 我收到错误AttributeError: 'Response' object has no attribute 'set_cookie'
    • 但是您的答案和我在问题中的代码的组合有所帮助;)。谢谢。
    • 有人有指向 set_cookie 文档的链接吗?找不到。
    • 有趣的是我如何一次又一次地遇到老问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多