码迷,mamicode.com
首页 > 编程语言 > 详细

python 监测window任务管理器某一进程状态,并邮件提醒

时间:2021-06-02 11:42:46      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rom   adl   pre   def   ESS   sendmail   smt   return   imp   

import os
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = ‘2575125xxx@qq.com‘
receivers = [‘13261xxxx@163.com‘]

message = MIMEText(‘Python 邮件发送...监测到程序已退出登录了,请尽快检查一下。‘, ‘plain‘, ‘utf-8‘)
message[‘From‘] = Header(sender, ‘utf-8‘)   # 发送者
message[‘To‘] =  Header("132617xxxx@163.com", ‘utf-8‘)        # 接收者

subject = ‘Python监测Windows信息‘
message[‘Subject‘] = Header(subject, ‘utf-8‘)


def isRunning(process_name) :
    try:
        print(‘tasklist | findstr ‘+process_name)
        process=len(os.popen(‘tasklist | findstr ‘+process_name).readlines())
        print(process)
        if process >=1 :
            return True
        else:
            return False
    except:
        print("程序错误")
        return False

def sendemail():
    try:
        server = smtplib.SMTP(‘smtp.qq.com‘,25)
        server.ehlo()
        server.starttls()
        server.login(sender , "授权码")
        text = message.as_string()
        server.sendmail(sender, receivers, text)
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print(e)
        print("Error")

if __name__=="__main__":
    flag=True
    while True:
        flag = isRunning("需要监测的程序.exe")
        print(flag)
        if flag == False:
            sendemail()
        time.sleep(60)##每隔60s进行检查

 

python 监测window任务管理器某一进程状态,并邮件提醒

标签:rom   adl   pre   def   ESS   sendmail   smt   return   imp   

原文地址:https://www.cnblogs.com/qxh-beijing2016/p/14814289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!