gpt4 book ai didi

python - 如何停止 pyftpdlib FTP 服务器?

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:13 24 4
gpt4 key购买 nike

我刚开始使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com