- 可以使用psutil包获取cpu、内存等信息
- 使用qq邮箱发送,需要使用ssl协议,465端口,即smtplib.SMTP_SSL(host, 465)
import smtplib,time,wmi,psutil
import logging;logging.basicConfig(level=logging.INFO)
from email.mime.text import MIMEText
def send(content, detials):
msg = MIMEText(content+'\nmem total:'+(str)(detials[0])+'\ncpu total:'+(str)(detials[1]))
logging.debug(msg.as_string())
msg['Subject'] = subject
msg['From'] = e_from
msg['To'] = e_to
#qq
# s = smtplib.SMTP_SSL(host, port)
#163
s = smtplib.SMTP(host, port)
try:
s.login(e_from, password)
s.sendmail(e_from, e_to, msg.as_string())
logging.error('send success')
except Exception as e:
logging.error('send failure', e)
finally:
s.quit()
#qq to 163
# e_from = '*****@qq.com'
# password = '******'
# e_to = '******@163.com'
# host = 'smtp.qq.com'
# port = 465
#163 to qq
e_from = '*****@163.com'
password = '*****'
e_to = '****@qq.com'
host = 'smtp.163.com'
port = 25
subject = 'cpu使用率通知'
# c = psutil.cpu_percent(1, True)
# print(psutil.virtual_memory())
# print('pc cpu:',psutil.cpu_percent(0))
# print('py process cpu:', psutil.cpu_percent(None))
# send(time_to_send)
i=2
while i>0:
time.sleep(100)
t = time.localtime(time.time())
hour_and_min = time.strftime('%d-%H:%M:%S')
#28-11:08:43
cp = psutil.cpu_percent(0)
detials = (psutil.virtual_memory(), cp)
logging.info(hour_and_min)
logging.info(detials[0])
logging.info('cpu:'+(str)(detials[1])+'\n')
if cp >3:
send(hour_and_min, detials)
i -= 1