【问题标题】:Add SameSite parameter to cookie in Python 2.7在 Python 2.7 中将 SameSite 参数添加到 cookie
【发布时间】:2020-03-02 05:58:00
【问题描述】:

如何在 Python 2.7 中将 SameSite 参数添加到 cookie?

我已经看到了这个How do I set the `SameSite` attribute of HTTP cookies in python?,但我不清楚这是否适用于 Python 2.7,或者我什至如何将它与我拥有的代码结合起来:

response.set_cookie(key="", value="", max_age="", expires="", path="/",domain="",secure=None,httponly=True)
return response

我使用 Django,所以我像这样创建响应:

response = render(request, "template.html", {})

【问题讨论】:

    标签: python cookies


    【解决方案1】:

    我想通了。我在 Python 2.7 中使用(旧版本的)Django,并将 cookie 添加到响应对象,如下所示:

    response["Set-Cookie"] = COOKIE_NAME+'='+COOKIE_VALUE+';expires='+EXPIRES+';Secure;SameSite=None;HttpOnly;Path=/;domain='+MY_DOMAIN+';'
    

    请注意,您只能像这样设置一次,因为该值会被覆盖。大概您可以设置多个cookie,但不需要它。你仍然可以使用 response.set_cookie() 设置 cookie,至少在你这样做之前。

    过期了,我是这样的:

    max_age = 315360000
    expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2014-07-17
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2011-06-08
      相关资源
      最近更新 更多