In one of my projects there was the need to provide an ftp file transfer. So instead of setting up a ftp server I decided to make one of my own using the pyftpdlib library and add custom actions once a file was received or transferred. The other advantage was the fact that the ftp server would have been part of the existing project’s python software.
Here’s an example in case you want a custom handler and you don’t have the time to read the ftpserver.py source.
from pyftpdlib import ftpserver from threading import Thread class PythoFtpServer(Thread): def __init__(self): Thread.__init__(self) self.daemon = True authorizer = ftpserver.DummyAuthorizer() authorizer.add_user("usr","pwd", "adir",perm='elradfmw') handler = CustomFtpHandler handler.authorizer = authorizer address = ('127.0.0.1',1024) self.server = ftpserver.FTPServer(address, handler) def run(self): Thread.run(self) self.server.serve_forever() class CustomFtpHandler(ftpserver.FTPHandler): def on_file_sent(self, file): """Called every time a file has been succesfully sent. "file" is the absolute name of the file just being sent. """ def on_file_received(self, file): """Called every time a file has been succesfully received. "file" is the absolute name of the file just being received. """ def on_incomplete_file_sent(self, file): """Called every time a file has not been entirely sent. (e.g. ABOR during transfer or client disconnected). "file" is the absolute name of that file. """ def on_incomplete_file_received(self, file): """Called every time a file has not been entirely received (e.g. ABOR during transfer or client disconnected). "file" is the absolute name of that file. """ def on_login(self, username): """Called on user login.""" def on_login_failed(self, username, password): """Called on failed user login. At this point client might have already been disconnected if it failed too many times. """ def on_logout(self, username): """Called when user logs out due to QUIT or USER issued twice."""
Thanks to Giampaolo Rodola for this library 🙂