v0631532d32d11c_r.jpg

MojoWebQQ容易自动停止客户端,因此在停止时增加通知功能。

#!/usr/bin/python
# -*- coding:utf-8 -*-

import os,sys
import pyinotify
from functions import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

tmp='/tmp'
QRCODE='/tmp/mojo_webqq_qrcode_default.png'

def smail(mailfrom, receivers, subject, body, att):
    sender = 'sender@test.com'
    mailtotag = 'devops all'
    smtpserver = 'smtp.test.com'
    username = 'sender@test.com'
    password = 'app_Test123456'

    # 创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = Header(mailfrom, 'utf-8')
    message['To'] = Header(mailtotag, 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')

    # 邮件正文内容
    if isinstance(body, unicode):
        body = str(body)
    message.attach(MIMEText(body, 'plain', 'utf-8'))

    # 构造附件1,传送当前目录下的 test.txt 文件
    att1 = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    att1["Content-Disposition"] = 'attachment; filename=二维码.png'
    message.attach(att1)

    att2 = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename=att'
    # message.attach(att2)

    message["Accept-Language"] = "zh-CN"
    message["Accept-Charset"] = "ISO-8859-1,utf-8"

    try:
        smtpObj = smtplib.SMTP(smtpserver)
        smtpObj.login(username, password)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print "mail send"
    except smtplib.SMTPException:
        print "Error: send failed"


class OnIOHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print('Action', "create file: %s " % os.path.join(event.path, event.name))
    if os.path.join(event.path, event.name)=='/tmp/mojo_webqq_qrcode_default.png':
        mailfrom = 'sender@test.com'
            receivers = ['me@test.com', 'me1@test.com']  # 接收邮件
            subject = 'QQ二维码登录'
            body = '手机扫码登录'
            att = os.path.join(event.path, event.name)
            smail(mailfrom, receivers, subject, body, att)

    def process_IN_DELETE(self, event):
        print('Action', "delete file: %s " % os.path.join(event.path, event.name))
    if os.path.join(event.path, event.name)=='mojo_webqq_pid_default.pid':
        mailfrom = 'sender@test.com'
                receivers = ['me@test.com', 'me1@test.com']  # 接收邮件
                subject = 'QQ已经离线'
                body = '请及时重新登录'
                att = ''
                smail(mailfrom, receivers, subject, body, att)

    def process_IN_MODIFY(self, event):
        print('Action', "modify file: %s " % os.path.join(event.path, event.name))


def auto_compile(path='.'):
    wm = pyinotify.WatchManager()
    mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
    notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())
    notifier.start()
    wm.add_watch(path, mask, rec=True, auto_add=True)
    print('Start Watch', 'Start monitoring %s' % path)
    while True:
        try:
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
        except KeyboardInterrupt:
            notifier.stop()
            break
            
if __name__ == "__main__":
   auto_compile(tmp)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code