Python 在twistd中使我的文件夹不可见/受限制的最佳方法是什么?
几天前,我试图学习python twisted….Python 在twistd中使我的文件夹不可见/受限制的最佳方法是什么?,python,twisted,Python,Twisted,几天前,我试图学习python twisted…. 这就是我制作Web服务器的方法: from twisted.application import internet, service from twisted.web import static, server, script from twisted.web.resource import Resource import os class NotFound(Resource): isLeaf=True def render(
这就是我制作Web服务器的方法:
from twisted.application import internet, service
from twisted.web import static, server, script
from twisted.web.resource import Resource
import os
class NotFound(Resource):
isLeaf=True
def render(self, request):
return "Sorry... the page you're requesting is not found / forbidden"
class myStaticFile(static.File):
def directoryListing(self):
return self.childNotFound
#root=static.file(os.getcwd()+"/www")
root=myStaticFile(os.getcwd()+"/www")
root.indexNames=['index.py']
root.ignoreExt(".py")
root.processors = {'.py': script.ResourceScript}
root.childNotFound=NotFound()
application = service.Application('web')
sc = service.IServiceCollection(application)
i = internet.TCPServer(8080, server.Site(root))#@UndefinedVariable
i.setServiceParent(sc)
在我的代码中,我为twisted.web.static.File创建了一个实例类,并覆盖目录列表
所以当用户尝试访问我的资源文件夹时(http://localhost:8080/resource/
或http://localhost:8080/resource/css
),它将返回一个未找到的页面。
但他仍然可以打开/阅读http://localhost:8080/resource/css/style.css
它是有效的
我想知道的是。。这是正确的方法吗???
还有其他“完美”的方法吗?
我正在寻找一个禁用directoryListing的配置,比如root.dirListing=False
。但是没有运气…是的,这是一个合理的方法。您还可以使用twisted.web.resource.NoResource
或twisted.web.resource.forbidded
而不是定义自己的NotFound