Python Tornado 中的多个静态目录

发布于 2024-09-27 16:56:22 字数 156 浏览 6 评论 0原文

我的目录结构设置如下:

root/
  js/
  css/
  libs/
  index.html

从 Tornado 中,我想将 js、css 和 libs 作为静态目录提供服务,但我只能找出如何提供其中之一。这可以做到吗?

I have a directory structure setup like:

root/
  js/
  css/
  libs/
  index.html

From Tornado, I want to serve js, css, and libs as static directories, but I can only find out how to serve one of them. Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南笙 2024-10-04 16:56:22

请参阅 https://stackoverflow.com/a/10165739/1813988

您可以通过设置处理程序来设置不同资产的静态路径像这样(并删除应用程序中的 static_path 设置):

处理程序 = [
            (r'/favicon.ico',tornado.web.StaticFileHandler,{'path':favicon_path}),
            (r'/static/(.*)',tornado.web.StaticFileHandler, {'path': static_path}),
            (r'/',WebHandler)
]

See https://stackoverflow.com/a/10165739/1813988

You can set the static path for different assets by setting handlers like this (and remove the static_path setting in Application):

handlers = [
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path':  favicon_path}),
            (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
            (r'/', WebHandler)
]
月下伊人醉 2024-10-04 16:56:22

不,这是不可能的。

您当然可以创建一个新文件夹——parent,并将js、css和libs放入该文件夹中,然后将该父文件夹指定为“static_path”

nb。 “在生产中,您可能希望从更优化的静态文件服务器(例如 nginx)提供静态文件”

No its not possible.

You could ofcourse create a new folder -- parent, and place js, css and libs inside of that folder, and then speciy that parent folder as the 'static_path'

nb. "In production, you probably want to serve static files from a more optimized static file server like nginx"

以为你会在 2024-10-04 16:56:22

正如 Schildmeijer 从 Tornado 网站引用的那样,我建议使用 Nginx 来提供静态文件。尽早进行此设置非常方便且简单。这还可以让您在未来获得一些其他潜在的好处:

  • 使用 Nginx 进行负载平衡
  • 使用 Nginx 处理 SSL

As Schildmeijer quoted from the Tornado website, I recommend using Nginx to serve static files. Having this setup early on is very convenient and easy. This also allows you some other potential benefits in the future:

  • Using Nginx for load balancing
  • Using Nginx to handle SSL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文