【问题标题】:How do I set the `SameSite` attribute of HTTP cookies in python?如何在 python 中设置 HTTP cookie 的“SameSite”属性?
【发布时间】:2018-11-21 14:39:32
【问题描述】:

Support for Same-Site cookies has landed in Firefox 60,但从 Python 3.6 开始,标准库 cookie 模块不支持 SameSite 属性。

【问题讨论】:

    标签: python http cookies


    【解决方案1】:

    SameSite 属性的支持已于 2018 年 4 月 7 日在 Pull Request #6413 中添加。

    可以对旧版本进行猴子补丁以支持该属性:

    try:
        from http.cookies import Morsel
    except ImportError:
        from Cookie import Morsel
    
    Morsel._reserved[str('samesite')] = str('SameSite')
    

    或者使用six:

    from six.moves.http_cookies import Morsel
    
    Morsel._reserved[str('samesite')] = str('SameSite')
    

    【讨论】:

    • 为什么在samesiteSameSite 周围出现str
    • unicode_literals模式开启时确保与python 2的兼容性。
    • 感谢您的澄清;这有帮助,因为我使用 python 3,所以我删除了 str
    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 2020-04-25
    • 2019-10-31
    • 2020-04-18
    • 2019-06-03
    • 2020-09-24
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多