我刚开始使用pyftpdlib。我首先要做的是使用示例服务器并将其包装在线程中。但是,如果我运行此脚本,将不会退出:
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import logging
import threading
class FtpThread(threading.Thread):
def __init__(self, user_list):
self.user_list = user_list
self.killswitch = 0
threading.Thread.__init__(self)
def run(self,*args,**kwargs):
authorizer = DummyAuthorizer()
authorizer.add_user('user', 'bla', '/home/user/git/verteilerCore2/default_basedir/in/1/', perm='elradfmwM')
handler = FTPHandler
handler.authorizer = authorizer
logging.basicConfig(filename='/var/log/verteiler_ftp.log', level=logging.INFO)
handler.banner = "pyftpdlib based ftpd ready."
address = ('', 21)
self.server = FTPServer(address, handler)
self.server.max_cons = 256
self.server.max_cons_per_ip = 5
self.server.serve_forever()
def stop(self):
self.server.close_all()
def main():
ftp_server = FtpThread("")
ftp_server.start()
import time
time.sleep(1)
ftp_server.stop()
if __name__ == '__main__':
main()
我必须怎么做才能使server_forever停止?调用“close_all”可以停止服务器的服务,但是整个脚本不会退出:(
最佳答案
我遇到了同样的问题(python 2.7.14,Ubuntu 16.04)。如https://github.com/giampaolo/pyftpdlib/blob/master/pyftpdlib/test/test_servers.py中的建议,我通过使用ThreadedFTPServer iso FTPServer使它正常工作。
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import ThreadedFTPServer
import threading
def __init__(self, addr):
authorizer = DummyAuthorizer()
authorizer.add_anonymous(os.getcwd())
handler = FTPHandler
handler.authorizer = authorizer
handler.banner = "pyftpdlib based ftpd ready."
ThreadedFTPServer.__init__(self, addr, handler)
# set a limit for connections
self.max_cons = 256
self.max_cons_per_ip = 5
def _run_server(self):
self.serve_forever()
def start(self):
srv = threading.Thread(target=self._run_server)
srv.deamon = True
srv.start()
def stop(self):
self.close_all()
->在这种情况下,调用“close_all”退出脚本
关于python - 如何停止pyftpdlib FTP服务器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26599859/