【问题标题】:get cookies with tornado StaticFileHandler使用 tornado StaticFileHandler 获取 cookie
【发布时间】:2014-12-25 12:42:24
【问题描述】:

我想在他获取一些 js 静态文件时获取用户的会话 cookie:

class StaticFileHandler(web.StaticFileHandler):
    @gen.coroutine
    def get(self, path):
        print ('got JS request from %s' % self.request.remote_ip)
        print ('request.cookies : %s' % self.request.cookies)
        super(StaticFileHandler, self).get(path)

但是,cookie 始终为None,即使我可以在浏览器中看到它设置了会话 cookie。

我在这里做错了什么?提供这样的静态文件时是否无法获取 cookie?

谢谢

【问题讨论】:

    标签: python-3.x cookies tornado


    【解决方案1】:

    tornado 文档警告不要覆盖 StaticFileHandler get 方法:http://tornado.readthedocs.org/en/latest/web.html#tornado.web.StaticFileHandler

    “子类应该只覆盖本节中讨论的方法;覆盖其他方法很容易出错。覆盖 StaticFileHandler.get 由于与 compute_etag 和其他方法的紧密耦合而特别成问题。”

    我建议您尝试覆盖 StaticFileHandler 类的 get_content 方法:

    http://tornado.readthedocs.org/en/latest/web.html#tornado.web.StaticFileHandler.get_content

    【讨论】:

    • 谢谢。我并没有真正更改 get 方法中的任何内容,只是添加了两三行逻辑并调用原始 get,所以我看不出这有什么问题。另外,我最终通过将 cookie 设置为 url 参数解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2015-06-15
    • 2018-12-24
    相关资源
    最近更新 更多