python模块------pyinotify

时间:2024-09-18 18:08:14

介绍

pyinotify模块用来监测文件系统的变化,依赖于Linux内核的inotify功能,inotify是一个事件驱动的通知器,其通知接口从内核空间到用户空间通过三个系统调用。pyinotify结合这些系统调用,提供一个*的抽象和一个通用的方式来处理这些功能。

Linux在2.6.13版本以后才提供了inotify。

pyinotify模块API

Notifier是pyinotify模块最重要的类,用来读取通知和处理事件,默认情况下,Notifier处理事件的方式是打印事件。

Notifier类在初始化时接受多个参数,但是只有WatchManager对象是必须传递的参数,WatchManager对象保存了需要监视的文件和目录,以及监视文件和目录的哪些事件,Notifier类根据WatchManager对象中的配置来决定如何处理事件。

#!/usr/bin/env python3

import pyinotify

wm = pyinotify.WatchManager()              # 创建WatchManager对象
wm.add_watch('/tmp',pyinotify.ALL_EVENTS) # 添加要监控的目录,以及要监控的事件,这里ALL_EVENT表示所有事件 notifier = pyinotify.Notifier(wm) # 交给Notifier进行处理
notifier.loop() # 循环处理事件

事件标志

pyinotify 仅仅是对 inotify 的Python封装,inotify提供了多种事件,基本上事件名称和含义都是相同的。常用的事件标志有:

python模块------pyinotify

上面列举的是事件的标志位,我们可以用'与'来关联监控多个事件。

事件处理

前面的例子,我们使用的都是默认的事件处理方式:打印在屏幕上,而很多情况下我们需要定制事件的处理方式,来实现特殊的功能。

定制事件需要继承ProcessEvent类,针对需要处理的事件编写 process_事件名(self,event) 函数即可

import pyinotify

multi_event = pyinotify.IN_OPEN | pyinotify.IN_CLOSE_NOWRITE    # 监控多个事件
wm = pyinotify.WatchManager() # 创建WatchManager对象 class MyEventHandler(pyinotify.ProcessEvent): # 定制化事件处理类,注意继承
def process_IN_OPEN(self,event): # 必须为process_事件名称,event表示事件对象
print('OPEN',event.pathname) # event.pathname 表示触发事件的文件路径
def process_IN_CLOSE_NOWRITE(self,event):
print('CLOSE_NOWRITE',event.pathname) handler = MyEventHandler() # 实例化我们定制化后的事件处理类
notifier = pyinotify.Notifier(wm,handler) # 在notifier实例化时传入,notifier会自动执行 wm.add_watch('/tmp',multi_event) # 添加监控的目录,及事件
notifier.loop()

实战

需求:

利用python编写监控程序,监控一个文件目录,当目录下的文件发生改变时,实现有修改就发报警邮件

邮件使用QQ邮箱,需要开启smtp,使用手机发生短信,腾讯会给你发邮箱密码。

过程:

import os
from pyinotify import WatchManager, Notifier,ProcessEvent,IN_DELETE, IN_CREATE,IN_MODIFY class EventHandler(ProcessEvent):
def process_IN_CREATE(self, event):
print "Create file: %s " % os.path.join(event.path,event.name)
def process_IN_DELETE(self, event):
print "Delete file: %s " % os.path.join(event.path,event.name) def process_IN_MODIFY(self, event):
print "Modify file: %s " % os.path.join(event.path,event.name) def FSMonitor(path):
wm = WatchManager()
mask = IN_DELETE | IN_CREATE |IN_MODIFY
notifier = Notifier(wm, EventHandler())
wm.add_watch(path, mask,auto_add=True,rec=True)
print 'now starting monitor %s'%(path)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break if __name__ == "__main__":