2017-12-15 3 views
0

수신 FTP 명령을 기록하는 방법을 나는 FTP 서버로 꼬여 사용하고 있습니다?트위스트 서버

+0

이 예는 도움이됩니까? http://twistedmatrix.com/documents/current/_downloads/ftpclient.py – BoboDarph

답변

1

FTPRealm은 파일 시스템에 대한 액세스를 조정하기 위해 FTPAnonymousShellFTPShell 인스턴스 (아바타)를 만듭니다.

이 클래스들은 모두 IFTPShell을 구현합니다. 한 가지 해결책은 작성한 아바타에 로깅 래퍼를 적용하는 FTPRealm의 래퍼를 만드는 것입니다.

from twisted.python.components import proxyForInterface 

class WrappingRealm(proxyForInterface(IRealm)): 
    wrap = staticmethod(logging_wrapper) 

    def requestAvatar(self, *a, **kw): 
     d = maybeDeferred(
      super(WrappingRealm, self).requestAvatar, 
      *a, **kw 
     ) 
     def got_avatar((iface, avatar, logout)): 
      return (iface, self.wrap(avatar), logout) 
     d.addCallback(got_avatar) 
     return d 

과 같은 logging_wrapper 뭔가 구현 : 당신은 인터페이스의 각 방법에 대한 로깅을 추가 할 필요가 있기 때문에

class _LoggingFTPShell(proxyForInterface(IFTPShell)): 
    def makeDirectory(self, path): 
     log(avatar=self.avatar, operation="makeDirectory", path=path) 
     return super(_LoggingFTPShell, self).makeDirectory(path) 

    # The same for the rest of the methods of IFTPShell 
    # ... 

def logging_wrapper(avatar): 
    return _LoggingFTPShell(avatar) 

이 조금 지루한입니다. 그러나 Twisted의 FTP 프로토콜 구현은 원하는 로깅을 수행 할 수있는 기능을 기본적으로 제공하지 않기 때문에이 문제를 해결하기가 어렵습니다. 일부 메타 프로그래밍을 사용하면 일부 타이핑을 줄일 수 있습니다 (그러나 약간의 복잡성을 감수해야 함).

또 다른 접근법은 관심있는 로깅을 추가하는 Twisted에 패치를 제공하는 것입니다.

관련 문제