如何为龙卷风服务器设置路径

How to set path for tornado server

我正在用 python 龙卷风构建服务器。我的同事已经完成了前端部分。

我做了一个非常简单的测试如下:

class IndexHandler(tornado.web.RequestHandler):
        def get(self):
            print("this is a get request from indexhandler:\n")
            print(self.request)
            self.render("frontend/index.html")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

对于此测试,浏览器得到 500: Internal Server Error。因为 index.html 在子目录中包含一些 js 文件。
这是服务器的消息:

[I 170430 23:06:21 web:2063] 200 GET / (108.61.177.156) 2.56ms
[W 170430 23:06:22 web:2063] 404 GET /css/reset.css (108.61.177.156) 0.56ms
[W 170430 23:06:22 web:2063] 404 GET /css/common.css (108.61.177.156) 0.56ms
[W 170430 23:06:22 web:2063] 404 GET /css/index.css (108.61.177.156) 0.41ms

这意味着浏览器获得了 index.html 但无法获得那些 css 文件。

所以我想我需要做这样的事情:

self.render("frontend/*")

我试过这样但失败了。

此外,如果我确实需要某种正则表达式来执行此操作,我认为这是非常危险的,因为用户可以从他的浏览器执行这样的请求:

www.mysite.com/../../someLocalFileOfServer

你看过tornado的文档吗?只需查看此文档以获取静态服务。 http://www.tornadoweb.org/en/stable/guide/running.html#static-files-and-aggressive-file-caching

更新

为什么会有危险?假设你有这样一个项目:

project
--static_dir
----test.js
----some other static files
--view
----index.html
----some other view files
--server.py

您可以将 static dir 设置为 static_dir,然后用户将可以通过 url domian/test.js 访问 test.js。所以用户可以访问什么由你决定。