python实现服务器程序报警,以及解决自动发送邮件在linux系统中报错问题

python实现服务器程序报警,以及解决自动发送邮件在linux系统中报错问题

脚本实现监测服务器程序运行情况,运行出错报警,向相关人员发送邮件

代码实现:

1.自动发邮件类

'''
自动发送邮件接口
'''

import smtplib
import email
# 负责构造文本
from email.mime.text import MIMEText
# 负责构造图片
from email.mime.image import MIMEImage
# 负责将多个对象集合起来
from email.mime.multipart import MIMEMultipart
from email.header import Header


class Auto_Send_Error_Mail:
    # 初始化该类
    def __init__(self, mail_host, mail_sender, mail_license, mail_receivers, body_content):
        self.mail_host = mail_host
        self.mail_sender = mail_sender
        self.mail_license = mail_license
        self.mail_receivers = mail_receivers
        self.body_content = body_content

    # 发送错误日志邮件
    def send_email(self):
        mm = MIMEMultipart('related')
        # 邮件主题
        subject_content = """油机爬虫错误日志"""
        # 设置发送者,注意严格遵守格式,里面邮箱为发件人邮箱
        mm["From"] = "sender_name<18055525400@163.com>"
        # 设置接受者,注意严格遵守格式,里面邮箱为接受者邮箱
        mm["To"] = "receiver_1_name<1165476886@qq.com>"
        # 设置邮件主题
        mm["Subject"] = Header(subject_content, 'utf-8')

        # 邮件正文内容
        # body_content = """你好,这是一个测试邮件!"""
        # 构造文本,参数1:正文内容,参数2:文本格式,参数3:编码方式
        message_text = MIMEText(self.body_content, "plain", "utf-8")
        # 向MIMEMultipart对象中添加文本对象
        mm.attach(message_text)
        '''
        这里特别强调:
        	在服务器linux系统上,检验会更加的严格,所以采用SMTP_SSL()
        	python3.7以上,smtplib模块文件做了修改
        	需要改写为:
        		端口25,ssl端口为465
        		smtplib.SMTP_SSL(self.mail_host).connect(self.mail_host, 465)
        	版本以下:
        		smtplib.SMTP_SSL().connect(self.mail_host, 465)
        '''
        # stp = smtplib.SMTP()
        stp = smtplib.SMTP_SSL(self.mail_host)
        # 设置发件人邮箱的域名和端口,端口地址为25,ssl端口为465
        stp.connect(self.mail_host, 465)
        # set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息
        stp.set_debuglevel(1)
        # 登录邮箱,传递参数1:邮箱地址,参数2:邮箱授权码
        stp.login(self.mail_sender, self.mail_license)
        # 发送邮件,传递参数1:发件人邮箱地址,参数2:收件人邮箱地址,参数3:把邮件内容格式改为str
        stp.sendmail(self.mail_sender, self.mail_receivers, mm.as_string())
        print("邮件发送成功")
        # 关闭SMTP对象
        stp.quit()

# # SMTP服务器,这里使用163邮箱
# mail_host = "smtp.163.com"
# # 发件人邮箱
# mail_sender = "xxxxxxxxx@163.com"
# # 邮箱授权码,注意这里不是邮箱密码,如何获取邮箱授权码,请看本文最后教程
# mail_license = "BGDAEEMWDFDGBRVQ"
# # 收件人邮箱,可以为多个收件人
# mail_receivers = ["xxxxxxx@qq.com"]
#
# mm = MIMEMultipart('related')

# 邮件主题
# subject_content = """Python邮件测试"""
# # 设置发送者,注意严格遵守格式,里面邮箱为发件人邮箱
# mm["From"] = "sender_name<xxxxxx@163.com>"
# # 设置接受者,注意严格遵守格式,里面邮箱为接受者邮箱
# mm["To"] = "receiver_1_name<xxxxx@qq.com>"
# # 设置邮件主题
# mm["Subject"] = Header(subject_content, 'utf-8')



# 邮件正文内容
# body_content = """你好,这是一个测试邮件!"""
# # 构造文本,参数1:正文内容,参数2:文本格式,参数3:编码方式
# message_text = MIMEText(body_content, "plain", "utf-8")
# # 向MIMEMultipart对象中添加文本对象
# mm.attach(message_text)

# 二进制读取图片
# image_data = open('a.jpg', 'rb')
# # 设置读取获取的二进制数据
# message_image = MIMEImage(image_data.read())
# # 关闭刚才打开的文件
# image_data.close()
# # 添加图片文件到邮件信息当中去
# mm.attach(message_image)

# 构造附件
# atta = MIMEText(open('recognition.log', 'r').read(), 'base64', 'utf-8')
# # 设置附件信息
# atta["Content-Disposition"] = 'attachment; filename="sample.log"'
# # 添加附件到邮件信息当中去
# mm.attach(atta)

# 创建SMTP对象
# stp = smtplib.SMTP()
# # 设置发件人邮箱的域名和端口,端口地址为25
# stp.connect(mail_host, 25)
# # set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息
# stp.set_debuglevel(1)
# # 登录邮箱,传递参数1:邮箱地址,参数2:邮箱授权码
# stp.login(mail_sender, mail_license)
# # 发送邮件,传递参数1:发件人邮箱地址,参数2:收件人邮箱地址,参数3:把邮件内容格式改为str
# stp.sendmail(mail_sender, mail_receivers, mm.as_string())
# print("邮件发送成功")
# # 关闭SMTP对象
# stp.quit()

2.日志模块

# 日志模块
def write_log(log_content):
    time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    log_content1 = time_now + "---->" + log_content + os.linesep
    fp = open('recognition.log', 'a+')
    fp.write(log_content1)
    print('写入完成!')
    fp.close()

3.程序运行模块

write_log(str(e))
with open('recognition.log', 'r') as f:
    data_cotetnt = f.read()
print(data_cotetnt)
# SMTP服务器,这里使用163邮箱
mail_host = "smtp.163.com"
# 发件人邮箱
mail_sender = "xxxxxxx@163.com"
# 邮箱授权码,注意这里不是邮箱密码,如何获取邮箱授权码,请看本文最后教程
mail_license = "xxxxxxxx"
# 收件人邮箱,可以为多个收件人
mail_receivers = ["xxxxxxx@qq.com"]
from auto_send_mail import Auto_Send_Error_Mail

mail_obj = Auto_Send_Error_Mail(mail_host, mail_sender, mail_license, mail_receivers, data_cotetnt)
mail_obj.send_email()
原文地址:https://www.cnblogs.com/godlover/p/14236858.html