Aay*_*ora 5 python ftp ftplib python-watchdog
我想在 FTP 目录中添加新文件后立即将文件从 FTP 服务器获取到本地。
我知道可以使用看门狗观察器看到本地计算机上目录的更改。
但我想检查 FTP 服务器上目录的更改(添加新文件、删除文件)。
如何实现这一目标?
我用来检查本地计算机上目录更改的代码:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import ftplib
import time
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event):
print "Got event for file %s" % event.src_path
session = ftplib.FTP('address','username','password')
path='/directory/to/check'
session.cwd(path)
observer = Observer()
event_handler = ExampleHandler()
observer.schedule(event_handler, path_of_the_directory)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Run Code Online (Sandbox Code Playgroud)
FTP 协议没有 API 来通知客户端有关更改的信息。
如果 FTP 是连接远程文件系统的唯一接口,则唯一的解决方案是定期轮询 FTP 文件夹以了解更改。