2

数日前、私はpython twisted..を学ぼうとしました..
そしてこれが私が私のウェブサーバーを作る方法です:

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)

override私のコードでは、twisted.web.static.File とdirectoryListing.
そのため、ユーザーがリソース フォルダー (http://localhost:8080/resource/またはhttp://localhost:8080/resource/css) にアクセスしようとすると、notFound ページが返されます。
しかし、彼はまだ開く/読むことができますhttp://localhost:8080/resource/css/style.css.
できます...

私が知りたいのは..これは正しい方法ですか???
別の「完璧な」方法はありますか?
のような directoryListing を無効にする構成を探していましたroot.dirListing=False。しかし、運が悪い...

4

1 に答える 1

1

はい、それは合理的な方法です。独自の を定義する代わりにtwisted.web.resource.NoResourceorを使用することもできます。twisted.web.resource.ForbiddenNotFound

于 2012-05-04T13:44:40.120 に答える