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

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

问题描述

我希望linux机器(Raspberry pi)通过AFP监视共享文件夹(Apple文件协议,macbook是主机).

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

我可以通过mount_afp挂载共享文件夹,并安装了看门狗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.

如果主机(Apple Macbook)或其他PC修改了监视文件夹,则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.

在主机(Apple 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()

推荐答案

对于网络安装,通常不会发出通常的文件系统事件.在这种情况下,请尝试使用PollingObserver,而不是使用Observer,即从以下位置更改:

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类可以尝试.

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

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

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