新编码器在这里
我希望能够稍微清理一下这段代码。我希望能够将 smtplib 的内容移出课堂,但我仍然需要它来发送包含 pinotify 数据的电子邮件如果您查看我的代码,您会看到。这是非常多余的。
如果通知数据 > 发送包含文件创建数据的电子邮件
如果通知数据 > 发送包含文件已删除数据的电子邮件
我该如何巩固这一点。
import os, pyinotify, time, smtplib, string
from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
wm = WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # Watched Events
class PTmp(ProcessEvent):
def process_IN_CREATE(self,event):
output = "Created: %s " % os.path.join(event.path, event.name)
localtime = time.asctime( time.localtime(time.time()) )
final = output + localtime
SUBJECT = "Directory Changed"
TO = "user@localhost"
FROM = "user@domain.net"
text = final
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
), "\r\n")
s=smtplib.SMTP('localhost')
s.sendmail(FROM, TO, BODY)
s.quit()
def process_IN_DELETE(self,event):
output = "Removed: %s" % os.path.join(event.path, event.name)
localtime = time.asctime( time.localtime(time.time()) )
final = output + localtime
SUBJECT = "Directory Changed"
TO = "user@localhost"
FROM = "user@domain.net"
text = final
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
), "\r\n")
s=smtplib.SMTP('localhost')
s.sendmail(FROM, TO, BODY)
s.quit()
notifier=Notifier(wm, PTmp())
wdd=wm.add_watch('/var/test',mask,rec=True)
while True: # Loop Forever
try:
# process the queue of events as explained above
notifier.process_events()
if notifier.check_events():
# read notified events and enqeue them
notifier.read_events()
except KeyboardInterupt:
# Destroy the inotify's instance on this interupt(stop monitoring)
notiifier.stop()
break