统一引号格式,优化架构排布

This commit is contained in:
2024-09-26 15:16:57 +08:00
parent ff9d612e67
commit c5a03ad16f
22 changed files with 143 additions and 302 deletions

20
my_email/__init__.py Normal file
View File

@@ -0,0 +1,20 @@
# 尝试次数
TRY_TIMES = 3
# 最小等待时间(秒)
MIN_WAIT_TIME = 1
# 最大等待时间(秒)
MAX_WAIT_TIME = 3
# 程序异常短信配置
ERROR_EMAIL_CONFIG = {
# SMTP服务器地址
'smtp_server': 'smtp.163.com',
# 连接SMTP的端口
'port': 994,
# 发件人邮箱地址请确保开启了SMTP邮件服务
'sender': 'EchoLiu618@163.com',
# 授权码--用于登录第三方邮件客户端的专用密码,不是邮箱密码
'authorization_code': 'OKPQLIIVLVGRZYVH',
# 收件人邮箱地址
'receivers': ['1515783401@qq.com'],
}

77
my_email/error_email.py Normal file
View File

@@ -0,0 +1,77 @@
import datetime
import logging
import smtplib
from email.mime.text import MIMEText
from tenacity import retry, stop_after_attempt, wait_random
from log import HOSTNAME
from my_email import ERROR_EMAIL_CONFIG, TRY_TIMES, MIN_WAIT_TIME, MAX_WAIT_TIME
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME), reraise=True,
after=lambda x: logging.warning('发送邮件失败!'))
def send_email(email_config, massage):
smtp_server = email_config['smtp_server']
port = email_config['port']
sender = email_config['sender']
authorization_code = email_config['authorization_code']
receivers = email_config['receivers']
mail = smtplib.SMTP_SSL(smtp_server, port) # 连接SMTP服务
mail.login(sender, authorization_code) # 登录到SMTP服务
mail.sendmail(sender, receivers, massage.as_string()) # 发送邮件
mail.quit()
logging.info(f"成功发送了一封邮件到[{','.join(receivers)}]")
def send_error_email(program_name, error_name, error_detail):
"""
程序出错时发送邮件提醒
:param program_name: 运行的程序名
:param error_name: 错误名
:param error_detail: 错误的详细信息
:return:
"""
# SMTP 服务器配置
sender = ERROR_EMAIL_CONFIG['sender']
receivers = ERROR_EMAIL_CONFIG['receivers']
# 获取程序出错的时间
error_time = datetime.datetime.strftime(datetime.datetime.today(), '%Y-%m-%d %H:%M:%S:%f')
# 邮件内容
subject = f'【程序异常提醒】{program_name}({HOSTNAME}) {error_time}' # 邮件的标题
content = f'''<div class="emailcontent" style="width:100%;max-width:720px;text-align:left;margin:0 auto;padding-top:80px;padding-bottom:20px">
<div class="emailtitle">
<h1 style="color:#fff;background:#51a0e3;line-height:70px;font-size:24px;font-weight:400;padding-left:40px;margin:0">程序运行异常通知</h1>
<div class="emailtext" style="background:#fff;padding:20px 32px 20px">
<p style="color:#6e6e6e;font-size:13px;line-height:24px">程序:<span style="color:red;">【{program_name}】</span>运行过程中出现异常错误,下面是具体的异常信息,请及时核查处理!</p>
<table cellpadding="0" cellspacing="0" border="0" style="width:100%;border-top:1px solid #eee;border-left:1px solid #eee;color:#6e6e6e;font-size:16px;font-weight:normal">
<thead>
<tr>
<th colspan="2" style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;background:#f8f8f8">程序异常详细信息</th>
</tr>
</thead>
<tbody>
<tr>
<td style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;width:100px">异常简述</td>
<td style="padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px">{error_name}</td>
</tr>
<tr>
<td style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center">异常详情</td>
<td style="padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px">{error_detail}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
''' # 邮件的正文部分
# 实例化一个文本对象
massage = MIMEText(content, 'html', 'utf-8')
massage['Subject'] = subject # 标题
massage['From'] = sender # 发件人
receivers_str = ','.join(receivers)
massage['To'] = receivers_str # 收件人
send_email(ERROR_EMAIL_CONFIG, massage)