【问题标题】:tornado using RequestHandler.set_secure_cookie() doesn't allow adding samesite attribute使用 RequestHandler.set_secure_cookie() 的龙卷风不允许添加 samesite 属性
【发布时间】:2020-03-06 16:07:15
【问题描述】:

在我的 python 后端应用程序中,我试图设置一个具有有效龙卷风属性的安全 cookie。

但是,我遇到了一个验证码问题,说我需要设置属性 samesite=strict。

如下操作:

    # this line is called from another method that sets the cookie.
    request_handler.set_secure_cookie(**self.build_cookie())



    def build_cookie(self):
        cookie_info = {
            'name': 'session_cookie',
            'value': 'session_cookie_value',
            'httponly': True,
            'expires_days': None,
            'samesite': 'Strict',
            'secure': True,
        }
        return cookie_info

给我以下错误

File "/usr/local/lib/python3.6/http/cookies.py", line 332, in __setitem__
raise CookieError("Invalid attribute %r" % (K,))
http.cookies.CookieError: Invalid attribute 'samesite'

有人知道如何设置这个属性吗?

【问题讨论】:

    标签: python python-3.x cookies tornado samesite


    【解决方案1】:

    对 cookie 属性的支持取决于 Python 的版本; samesite 属性是在 Python 3.8 中添加的。您也可以按照this question 中的说明在旧版本的python 中对其进行猴子补丁。

    【讨论】:

    • 非常感谢,这对我有帮助
    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多