PyFire PyFire - 1 year ago 139
Python Question

PyFtpdLib How to add a user while still running server

I'm running a script for an FTP server in Python with PyFTPdLib like this:

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

def main():
authorizer = DummyAuthorizer()

authorizer.add_user('user', '12345', '.', perm='elradfmwM')

handler = FTPHandler
handler.authorizer = authorizer

address = ('localhost', 2121)
server = FTPServer(address, handler)

# start ftp server

if __name__ == '__main__':

Whenever I call this code, it keeps the command line busy with running the "server.serve_froever()", it doesn't keep running through the if loop.

So my question is, how do you add a user while the server is running without shutting down the server?

Do I need to create another script to call this one and then another to add a user? Will it need to be threaded so both can run without locking up on this one?
Iv'e tried to look through the tutorials on this module, but I haven't found any examples or seen any solutions. I'm willing to switch to a different module to run a FTP server if needed, so long as I can control everything.
I'm going to be adding users a lot from a database, then removing them. So I don't want to take the server down and restart it every time I need to add someone.

Answer Source

In case anyone was interested in this topic, I think I have found a solution.

What I did was save the above code in a file called "" and changed "main" from a def to a class.

#First .py module
class ftp_server:
   def __init__(self):
       self.authorizer = DummyAuthorizer()
       self.authorizer.add_user('admin', 'password', '.', perm='elradfmwM')

   def run(self):
       self.handler = FTPHandler
       self.handler.authorizer = self.authorizer
       self.address = ('localhost', 21)
       self.server = FTPServer(self.address, self.handler)
       logging.basicConfig(filename='pyftpd.log', level=logging.INFO)

   def add_user(self,user,passwd,loc,privi):
       self.authorizer.add_user(str(user), str(passwd), str(loc), perm=str(privi))

Then I made another module to call and run this file (really this is just for my personal taste, as you could just add this to the same module)

#Second .py module
import thread
import create_ftp_server

this_ftp = create_ftp_server.ftp_server()


All you have to do now is decide how you want to call the thread to add the user.

Note: I tried to use the newer Threading module but I couldn't get any real results with that. This option will work well for me, though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download