giampaolo / pyftpdlib

Extremely fast and scalable Python FTP server library
MIT License
1.65k stars 266 forks source link

Simulate FTP with serverless #546

Closed Abduvakilov closed 3 years ago

Abduvakilov commented 3 years ago

I have cameras that support FTP. I want to create a server that can be connected via FTP and receive files sent from the cameras. The received files would be processed and disposed. I need an FTP server which implements only the commands that are required for login and receiving the files. Other commands such as mkdir, ls might be ignored or simulated with database if needed.

I think I might need storage or at least a database to implement it. Is it possible to simulates FTP in such a manner on AWS Lambda or other serverless services?

giampaolo commented 3 years ago

To disable commands you can do it in your subclass:

from pyftpdlib.handlers import FTPHandler

class Handler(FTPHandler):

    def _not_implemented(self, *args, **kw):
        self.respond('500 Command not implemented.')

    ftp_MKD = _not_implemented
    ftp_RMD = _not_implemented
    ftp_DELE = _not_implemented

To make those commands interact with AWS instead of a real filesystem you'll have to integrate it in the filesystem class. Pseudo code:

from pyftpdlib.filesystems import AbstractedFS

class AwsFS(AbstractedFS):

    def open(...):
        # return a file-like object with read() and write() methods which 
        # interacts with AWS API
        ...

    def listdir(...):
        # ditto
        ...

In this case (AWS integration) you'll probably want to use a multi-thread server (ThreadedFTPServer class instead of FTPServer, see the doc) since the I/O will be blocking.