小编Aay*_*ora的帖子

如何检查 FTP 服务器上的目录是否发生变化?

我想在 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)

python ftp ftplib python-watchdog

5
推荐指数
1
解决办法
4199
查看次数

标签 统计

ftp ×1

ftplib ×1

python ×1

python-watchdog ×1