如何为 pylons 静态(公共)文件设置自定义响应标头?
如何向 pylons 公开提供的文件添加自定义标头?
How do I add a custom header to files pylons is serving from public?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何向 pylons 公开提供的文件添加自定义标头?
How do I add a custom header to files pylons is serving from public?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
a) 让您的网络服务器从 /public 而不是 Paster 提供文件,并将其配置为传递一些特殊标头。
b) 添加一个特殊的路由并自己提供文件
c) 也许有一种方法覆盖标题,我只是不知道如何。
a) Let your webserver serve files from /public instead of paster and configure it to pass some special headers.
b) Add a special route and serve the files yourself ala
c) maybe there is a way to overwrite headers and i just dont know how.
对于最新版本的路线,您可以使用“Magic path_info ' 功能,并按照此处的文档编写控制器,以便它调用 Paster.DirectoryApp。
在我的项目中,我想提供公共目录中的任何文件(包括子目录),并以此作为控制器结束,以便能够覆盖 content_type :
With a recent version of route, you can use the 'Magic path_info' feature, and follow the documentation from here to write your controller so it calls paster.DirectoryApp.
In my project, I wanted to serve any file in the public directory, including subdirs, and ended with this as controller, to be able to override content_type :
在标准 Pylons 设置中,公共文件由 StaticUrlParser 提供。 这通常是在 config/middleware.py:make_app() 函数中设置的。
您需要像 Antonin ENFRUN 描述的那样对 StaticUrlParser 进行子类化,尽管将其称为控制器会令人困惑,因为它的用途不同。 在 config/middleware.py 的顶部添加类似以下内容:
然后将 config/middleware.py:make_app() 中的 StaticUrlParser 替换为 HeaderUrlParser
变为
In a standard Pylons setup, the public files are served from a StaticUrlParser. This is typically setup in your config/middleware.py:make_app() function
You need to subclass the StaticUrlParser like Antonin ENFRUN describes, though calling it a Controller is confusing because it's doing a different purpose. Add something like the following to the top of the config/middleware.py:
then replace StaticUrlParser in config/middleware.py:make_app() with HeaderUrlParser
becomes
使用 FileApp 进行流式传输的更简单方法,基于 pylon书。 下面的代码假设您的路由提供了
some_file_identifier
,但其他两个变量是“神奇的”(请参阅代码后的说明)。如果方法签名中有这些名称的变量,Pylons 会自动为您提供 wsgi
environ
和start_response
变量。 否则,您不需要设置或修改标头,但如果您这样做,您可以使用 FileApp 中内置的功能 < /a> 来实现这一点。A simpler way to use FileApp for streaming, based on the pylons book. The code below assumes your route provides
some_file_identifier
, but the other two variables are "magic" (see explanation after code).Pylons automatically gives you the wsgi
environ
andstart_response
variables if you have variables of those names in your method signature. You should not need to set or munge headers otherwise, but if you do you can use the abilities built in to FileApp to achieve this.