from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.internet import reactor
folder_list = ["Documents", "Downloads"]
class RootPage(Resource):
isLeaf = True
def render_GET(self, request):
print request.uri
new_request = request.uri[7:]
if len(new_request) <= 3:
ret = ""
for folders in folder_list:
ret += "<a href='%s'>%s</a></br>" % ("/files/" + folders.replace(" ", "-") , folders)
return ret
root = Resource()
#folders
root.putChild('files', RootPage())
for folders in folder_list:
root.putChild( folders.replace(" ", "-"), File("/home/user1/" + folders))
factory = Site(root)
reactor.listenTCP(3000, factory)
reactor.run()
最佳答案
问题
1) 在 .putChild()
调用中,您将文件夹 URL 建立为根的子级,而不是 /files
的子级。
2) 在 RootPage
类定义中,您将 isLeaf
设置为 True
。但是您正在创建一个内部节点,而不是叶节点。
解决方案
删除 isLeaf
行,并将其他相关行更改为:
root = Resource()
rootpage = RootPage()
#folders
root.putChild('files', rootpage)
for folders in folder_list:
rootpage.putChild( folders.replace(" ", "-"), File("/home/user1/" + folders))
factory = Site(root)
关于python - 扭曲的非默认根 HTTP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40825047/