【问题标题】:self.get_secure_cookie returns None, but cookie existsself.get_secure_cookie 返回无,但 cookie 存在
【发布时间】:2015-08-22 07:58:12
【问题描述】:

我在 Tornado 的 WebSocketHandler 中检查 cookie 时遇到一个奇怪的错误:

class MyHandler(WebSocketHandler):
    def open(self):
        print(self.get_secure_cookie("cookie"))

它返回 None,但在浏览器中我可以看到该 cookie(在另一个登录处理程序中我使用 self.set_secure_cookie("cookie", "123")

如何解决这个问题?

谢谢!

更新

如果我使用self.request.headers 我只有:

{
'Pragma': 'no-cache', 
'Sec-Websocket-Version': '13', 
'Connection': 'Upgrade', 
'Upgrade': 'websocket', 
'Host': 'localhost:8888', 
'Sec-Websocket-Key': 'key', 
'Cache-Control': 'no-cache', 
'User-Agent': 'u-a', 
'Sec-Websocket-Extensions': 'x-webkit-deflate-frame', 
'Origin': 
'http://127.0.0.1:8888'
}

【问题讨论】:

  • 你能提供更多关于这里发生了什么的信息吗?
  • @JakobWeisblat 我想检查,如果用户登录,当他使用网络套接字时
  • 该 cookie 设置的路径是什么?
  • @21Zoo 默认路径 - /,我在“登录”页面设置了 cookie。网络套接字聊天 - /
  • 您是否通过浏览器的开发工具检查 a) cookie 是否已设置以及 b) 是否在 websocket 请求期间发送?

标签: python cookies tornado


【解决方案1】:

在此处查看此答案:User authentication in tornado websocket application

self.request.headers 应该包含您要查找的内容。

【讨论】:

    【解决方案2】:

    我已经开始使用 wss:// 并且有效

    【讨论】:

    • 我必须从 127.0.0.1 而不是 localhost 访问页面才能使 cookie 起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多