2012-05-02 6 views
2

fews日前、私は
..ねじれのpythonを学ぶしようとしましたが、これは私が私のウェブサーバを作る方法です:私のコードで私のフォルダを非表示にする/ twistdで制限する最善の方法は何ですか?

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のインスタンスクラスを作ります。ファイルとoverridedirectoryListing
ユーザーが自分のリソースフォルダ(http://localhost:8080/resource/またはhttp://localhost:8080/resource/css)にアクセスしようとすると、notFoundページが返されます。
彼はまだを開いたり読むことができます。
それは...

これは正しい方法ですか?
もう1つの「完璧な」方法はありますか?
私はroot.dirListing=FalseのようなdirectoryListingを無効にする設定を探していました。運がない...

答えて

1

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

関連する問題