Reputation: 31663
I'm using twisted as a FTP Server:
from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB
from twisted.internet import reactor
p = Portal(FTPRealm('./'),
[AllowAnonymousAccess(), FilePasswordDB("pass.dat")])
f = FTPFactory(p)
reactor.listenTCP(21, f)
reactor.run()
How can i log every received FTP command from client?
Upvotes: 0
Views: 346
Reputation: 48335
FTPRealm
creates FTPAnonymousShell
and FTPShell
instances (avatars) to mediate access to the filesystem.
These classes both implement IFTPShell
. One solution is to create a wrapper for FTPRealm
which applies a logging wrapper to the avatars it creates.
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
And implement logging_wrapper
something like:
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)
This is a bit tedious since you have to add logging for each method on the interface. However, since Twisted's FTP protocol implementation doesn't natively provide facilities for performing the logging you want, it's hard to get around this. With some meta-programming you might save yourself some typing (but at the cost of some complexity).
Another approach would be to contribute a patch to Twisted adding the logging you're interested in.
Upvotes: 1