【问题标题】:How to check for change in the directory at FTP server?如何在 FTP 服务器上检查目录中的更改?
【发布时间】:2018-11-08 07:37:28
【问题描述】:

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

【问题讨论】:

    标签: python ftp ftplib python-watchdog


    【解决方案1】:

    FTP 协议没有用于通知客户端更改的 API。

    如果 FTP 是您与远程文件系统的唯一接口,唯一的解决方案是定期轮询 FTP 文件夹以进行更改。

    参见例如Monitor remote FTP directory

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多