可以使用psutil包获取cpu、内存等信息使用qq邮箱发送,需要使用ssl协议,465端口,即smtplib.SMTP_SSL(host, 465)
import smtplib,time,wmi,psutilimport logging;logging.basicConfig(level=logging.INFO)from email.mime.text import MIMETextdef send(content, detials):msg = MIMEText(content+'\nmem total:'+(str)(detials[0])+'\ncpu total:'+(str)(detials[1]))logging.debug(msg.as_string())msg['Subject'] = subjectmsg['From'] = e_frommsg['To'] = e_to#qq# s = smtplib.SMTP_SSL(host, port)#163s = 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 = '*****@'# password = '******'# e_to = '******@'# host = ''# port = 465#163 to qqe_from = '*****@'password = '*****'e_to = '****@'host = ''port = 25subject = '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=2while i>0:time.sleep(100)t = time.localtime(time.time())hour_and_min = time.strftime('%d-%H:%M:%S')#28-11:08:43cp = 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