[英]cherrypy - serve static files and force content-type
I'm building a small web-app with cherrypy. 我正在用Cherrypy构建一个小型Web应用程序。
In this app, I need to serve files in two different ways: 在此应用中,我需要以两种不同的方式提供文件:
application/octet-stream
mime type, to force the download. application/octet-stream
mime类型一起使用,以强制下载。 Currently, I added an exposed method to stream files, one at /document/xx
, returning the correct mime type, the other one at /download/xx
with the octet-stream mime type. 当前,我添加了一个公开方法来流文件,一个在
/document/xx
,返回正确的mime类型,另一个在/download/xx
八位字节流的mime类型。
But I want to avoid coding this myself. 但是我想避免自己编写代码。 It can only bring bugs and security issues.
它只能带来错误和安全问题。
tl;dr: How can I force cherrypy's tools.staticdir
to force download? tl; dr: 如何强制cherrypy的
tools.staticdir
强制下载?
See the full code of the app on github: https://github.com/aspyct/docrepo (note that it's still using the old 'config.ini' file, no config dictionary). 在github上查看该应用程序的完整代码: https : //github.com/aspyct/docrepo (请注意,它仍在使用旧的'config.ini'文件,没有配置字典)。
You can force it by providing content_types
to the tool, that maps file extensions to MIME types. 您可以通过向工具提供
content_types
来强制它,该工具将文件扩展名映射到MIME类型。 Like this. 像这样。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
},
'/static' : {
'tools.staticdir.on' : True,
'tools.staticdir.dir' : os.path.join(path, 'static'),
'tools.staticdir.content_types' : {'html': 'application/octet-stream'}
}
}
if __name__ == '__main__':
cherrypy.quickstart(config = config)
If you don't know the extension beforehand, take a look at the tool's source code . 如果您事先不知道扩展名,请查看该工具的源代码 。 There's barely two dozen of effective lines of code.
几乎没有两行有效的代码。 Just make your own fine-tuned tool of the purpose.
只需使自己的目的微调的工具即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.