我想弄清楚如何自定义使用cherrypy服务的静态内容。
目标是在路径以/ pub开头时按正常方式提供文件,但当路径以其他任何内容开头时,我想添加首先检查访问权限的自定义函数。
文档给了我足够的信息。这是我到目前为止所拥有的......
import cherrypy
from cherrypy.lib.static import serve_file
class Root(object):
# Somehow turn this into a handler for all files
def page(self):
if authorized(): # Check whether authenticated user may access the requested path
file_location = .... # Work out file location from request
print("Serving file from %s" % file_location)
return serve_file(file_location)
else:
return "Not Authorized"
if __name__ == '__main__':
serve_conf = {'/': {'tools.gzip.on': True},
'/pub': {
'tools.staticdir.on': True,
'tools.staticdir.dir': "/data/public",
'tools.staticdir.debug': True,
'log.screen': True
},
'/secure': { "PROBABLY CONFIGURE THE HANDLER HERE" }
}
cherrypy.quickstart(Root(), config=serve_conf)