AutoDL GPU 使用#
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函數。