【问题标题】:Using watchdog of python to monitoring afp shared folder from linux使用python的看门狗从linux监控afp共享文件夹
【发布时间】:2018-01-08 13:04:45
【问题描述】:

我想让 linux 机器(Raspberry pi)通过 AFP 监控共享文件夹(Apple 文件协议,macbook 是主机)。

我可以通过 mount_afp 挂载共享文件夹,并安装 watchdog python 库来监控共享文件夹。问题是看门狗只能监控来自 linux 机器本身的修改。

如果监控文件夹被主机(Apple macbook)或其他电脑修改,linux机器无法发现更改。没有日志出来。

我在主机(苹果macbook)上测试了同一个看门狗python文件后,我可以得到其他机器的每一个修改日志。

主机可以获取文件或文件夹的所有修改。但是其他机器(来宾机器)无法监控来自主机或其他人的文件修改。

看门狗状态正常吗?还是账号和权限有问题?

这是看门狗示例。

  import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class Watcher:
    DIRECTORY_TO_WATCH = "/path/to/my/directory"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print "Error"

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print "Received created event - %s." % event.src_path

        elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            print "Received modified event - %s." % event.src_path


if __name__ == '__main__':
    w = Watcher()
    w.run()

【问题讨论】:

  • 看门狗只能监控本地文件系统的变化。操作系统和远程文件系统不会通知有关更改。
  • @Klaus D 嗨,有没有 python 库来监控 AFP 共享文件夹?
  • 第一个问题是主机是否通知客户端。如果没有,就不会有任何图书馆。

标签: python linux raspberry-pi watchdog afp


【解决方案1】:

对于网络挂载,通常的文件系统事件并不总是被发出。在这种情况下,不要使用Observer,而是尝试使用PollingObserver——即,更改为:

   self.observer = Observer()

   from watchdog.observers.polling import PollingObserver
   ...
   self.observer = PollingObserver()

还有一个PollingObserverVFS 类你可以试验一下。

文档:https://pythonhosted.org/watchdog/api.html#module-watchdog.observers.polling

【讨论】:

  • 我尝试从 watchdog.observers 导入 PollingObserver 但导入错误。我检查了文档,然后更改为 from watchdog.observers.polling import PollingObserver 基本上,它的工作!!!
  • 代码示例已更新为从正确的模块导入。谢谢大家。
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
相关资源
最近更新 更多