使用python的看门狗从linux监控afp共享文件夹 [英] Using watchdog of python to monitoring afp shared folder from linux

查看:33
本文介绍了使用python的看门狗从linux监控afp共享文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让linux机器(树莓派)通过AFP(苹果文件协议,macbook是主机)监控共享文件夹.

I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).

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

I can mount shared folder by mount_afp, and installed watchdog python library to monitor a shared folder. The problem is that watchdog can monitor only modifications from linux machine itself.

如果某个监控文件夹被主机(苹果macbook)或其他电脑修改过,linux机器是查不到修改的.没有日志出来.

If a monitoring folder has been modified by the host machine(Apple macbook) or other pc, linux machine couldn't find out the change. No logs came out.

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

After I tested the same watchdog python file in host machine(Apple macbook), I can get every logs of modifications by other machine.

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

Host machine can get every modifications of file or folder. But other machine(guest machine) can not monitor modifications of file from host or others.

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

Is it normal status of watchdog? Or is there any problem in account and permission?

这是看门狗示例.

  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()

推荐答案

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

For network mounts, the usual filesystem events don't always get emitted. In such cases, instead of using Observer, try using PollingObserver -- i.e., change from:

   self.observer = Observer()

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

还有一个 PollingObserverVFS 类你可以试验.

There is also a PollingObserverVFS class you could experiment with.

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

这篇关于使用python的看门狗从linux监控afp共享文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆