2012-05-02 4 views
2

fews 일 전, 나는 ... 파이썬은 트위스트
을 배우고 노력이 내가 내 웹 서버 만드는 방법은 다음과 같습니다 내 코드에서폴더를 보이지 않게하거나 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 페이지가 반환됩니다.
그는 여전히 을 열거 나 읽을 수 있습니다.
그것은 작동합니다 ...

제가 알고 싶은 것은 .. 올바른 방법입니다. ???
다른 '완벽한'방법이 있습니까?
나는 root.dirListing=False과 같이 directoryListing을 비활성화하는 구성을 찾고있었습니다. 그러나 운은 ...

답변

1

네, 그렇게하는 것이 합리적입니다. 자신의 NotFound을 정의하는 대신 twisted.web.resource.NoResource 또는 twisted.web.resource.Forbidden을 사용할 수도 있습니다.

관련 문제