1

I try to develop a threaded FTP server in python. When a file is received, some of the processes may block the flow for a while and I don't want to block the entire server. Based on the pyftpdlib library it should be very simple. According to the official documentation "Changing the concurrency module is easy: you just need to import a substitute for FTPServer class" with ThreadedFTPServer.

Starting from the official examples I made a custom code which waits 2 second of each file before it ends the thread. In theory, it should open multiple threads and close each of them at the end. In practice, it waits for each thread to close before it will accept another file.

from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import ThreadedFTPServer
from pyftpdlib.authorizers import DummyAuthorizer
import os
import time


class MyHandler(FTPHandler):

   

    def on_file_received(self, file):
        # do something when a file has been received
        f = os.path.basename(file)
        print("start %s" % f)
        time.sleep(2)
        print("ended %s" % f)
        os.remove(file)


  

def main():
    authorizer = DummyAuthorizer()
    authorizer.add_user('test', 'test','/temp', perm='elradfmwM')

    handler = MyHandler
    handler.authorizer = authorizer
    server = ThreadedFTPServer(('', 21), handler)
    server.serve_forever()

    server.max_cons = 256
    server.max_cons_per_ip = 5

if __name__ == "__main__":
    main()

Thank you for any hint!!!

Daniel U
  • 11
  • 1

0 Answers0