2025年3月25日 星期二 甲辰(龙)年 月廿四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python程序报错自动发送到QQ邮箱

时间:04-07来源:作者:点击数:34

python程序报错自动发送到QQ邮箱

  • import datetime
  • import ssl
  • import smtplib
  • import time
  • from email.message import EmailMessage
  • import psutil
  • def bind_process(path_lst):
  • if type(path_lst) != list:
  • print('请传入列表结构的数据')
  • return 1
  • while True:
  • error_lst = []
  • process_list = psutil.pids()
  • for path in path_lst:
  • pd = 0
  • path = path.replace('\\', '/').replace('//', '/')
  • for process in process_list:
  • p = psutil.Process(process)
  • try:
  • name = p.cmdline()[1]
  • except Exception:
  • continue
  • if path == name:
  • pd = 1
  • break
  • if pd == 0:
  • error_lst.append(path)
  • if error_lst:
  • sr = ''
  • for err in error_lst:
  • sr += err + '\n'
  • sendEmail(sr)
  • time.sleep(3600)
  • else:
  • print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ' 程序无异常')
  • time.sleep(3600)
  • def sendEmail(msg):
  • key = 'xzczopvwcjzmdgjg'
  • EMAIL_ADDRESS = '19876511781@qq.com' # 接受邮件的邮箱地址
  • EMAIL_PASSWORD = key
  • smtp = smtplib.SMTP('smtp.qq.com', 25)
  • context = ssl.create_default_context()
  • sender = EMAIL_ADDRESS
  • receiver = EMAIL_ADDRESS
  • subject = "程序意外终止"
  • body = msg
  • msg = EmailMessage()
  • msg['subject'] = subject
  • msg['From'] = sender
  • msg['To'] = [receiver] #多个用户
  • msg.set_content(body)
  • with smtplib.SMTP_SSL("smtp.qq.com", 465, context=context) as smtp:
  • smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
  • smtp.send_message(msg)
  • print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ' 有程序意外停止 已发送到邮箱')
  • bind_process([ r"C:\Users\Administrator\Desktop\anjuke3\main2.py"]) # 需要检测的文件路径(crawl启动的 路径写到crawl)
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门