python打log代码记录

简单记录一下一个用于打log和调用smpt发邮件的技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import time
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
class Mylog:
def __init__(self):
time_info=str(time.strftime("%m_%d_%H", time.localtime()))
if not os.path.exists('./runlogs/'):
os.mkdir('./runlogs/')
self.file_name='./runlogs/'+time_info+'.log'
self.file=open(self.file_name, 'a')
def __del__(self):
self.file.close()
#添加日志记录
def add_log(self,lg):
time_info = str(time.strftime("%H-%M-%S --> ", time.localtime()))
self.file.write(time_info+lg)
self.file.write('\r')
self.file.flush()
#运行结束后发送邮件
def send_mail(self):
self.file.close()
from_addr = '' # 邮件发送账号
to_addrs = '' # 接收邮件账号
qqCode = '' # 授权码(这个要填自己获取到的)
smtp_server = 'smtp.qq.com'
smtp_port = 465
# 配置服务器
stmp = smtplib.SMTP_SSL(smtp_server, smtp_port)
stmp.login(from_addr, qqCode)
with open(self.file_name,'r') as f:
buffer=f.read()
# 组装发送内容
message = MIMEText(buffer, 'plain', 'utf-8') # 发送的内容
message['From'] = Header("autodl", 'utf-8') # 发件人
message['To'] = Header("me", 'utf-8') # 收件人
subject = 'AUTODL-运行结束'
message['Subject'] = Header(subject, 'utf-8') # 邮件标题
try:
stmp.sendmail(from_addr, to_addrs, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败--' + str(e))

使用时,用add_log替换print函数,最后在os.system('shutdown')前调用send_mail函数。
作者

Jhuoer Yen

发布于

2024-11-07

更新于

2024-11-07

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×