PyFtpdLib如何在仍然运行服务器的同时添加用户 [英] PyFtpdLib How to add a user while still running server

查看:178
本文介绍了PyFtpdLib如何在仍然运行服务器的同时添加用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyFTPdLib在Python中为FTP服务器运行脚本,

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
    server.serve_forever()

if __name__ == '__main__':
    main()

每当我调用此代码时,它都会使命令行忙于运行"server.serve_forever()",而不会一直通过if循环运行.

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

所以我的问题是,如何在服务器运行时添加用户而不关闭服务器?

我需要创建另一个脚本来调用该脚本,然后再创建一个脚本来添加用户吗?是否需要使用线程以便两者都可以运行而不会锁定该线程?

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?

我试图浏览有关本模块的教程,但是我没有找到任何示例或看到任何解决方案.只要愿意,我愿意切换到其他模块来运行FTP服务器.

I've 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.

推荐答案

如果有人对此主题感兴趣,我想我已经找到了解决方法.

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

我所做的就是将上面的代码保存在一个名为"create_ftp_server.py"的文件中,并将"main"从def更改为一个类.

What I did was save the above code in a file called "create_ftp_server.py" 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)
       self.server.serve_forever()

   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()

thread.start_new_thread(this_ftp.run,())
thread.start_new_thread(this_ftp.add_user,('user','password',".",'elradfmwM'))

您现在要做的就是确定如何调用线程以添加用户.

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

注意:我试图使用较新的Threading模块,但无法获得任何实际结果.不过,此选项对我来说效果很好.

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.

这篇关于PyFtpdLib如何在仍然运行服务器的同时添加用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆