欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱功能(實(shí)例代碼)

 更新時(shí)間:2019年11月11日 11:56:56   作者:WUYANGEZRA  
這篇文章主要介紹了Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱功能,具體內(nèi)容如下所示:

程序設(shè)計(jì)

實(shí)現(xiàn)代碼

cpu.py

# -*- coding: utf-8 -*-
import psutil
import time
from emailsender import txtMail
from log import myloggers
import gc
class mycpumonitor():
 # up是cpu監(jiān)控的閾值,默認(rèn)是90%
 def __init__(self, up=None):
  self.up = 90 if up is None else up
 def cpu_monitor(self):
  cpu_percent = psutil.cpu_percent()
  now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  if cpu_percent > self.up:
   filename = 'cpu.txt'
   with open(filename, 'w') as f: # 如果filename不存在會(huì)自動(dòng)創(chuàng)建, 'w'表示寫數(shù)據(jù),寫之前會(huì)清空文件中的原有數(shù)據(jù)!
    f.write(str(now) + "CPU使用率超過" + str(self.up) + "!!!!.\n")
    f.write(str(now) + "當(dāng)前CPU使用率" + str(cpu_percent) + "!!!\n")
   mail = txtMail()
   try:
    mail.txt_send_mail(filename="test.config", alarm=filename)
    temp_msg = "CPU超標(biāo),當(dāng)前CPU使用率:" + str(cpu_percent)
    logger1 = myloggers(temp_msg)
    logger1.maillogging()
    del mail
    gc.collect()
   except:
    print("文本文件格式不正確")

mem.py

# -*- coding: utf-8 -*-
import psutil
import time
from emailsender import txtMail
from log import myloggers
import gc
class mymemmonitor():
 # up是內(nèi)存監(jiān)控的閾值,默認(rèn)是90%
 def __init__(self, up=None):
  self.up = 90 if up is None else up
 def mem_monitor(self):
  mem_percent = psutil.virtual_memory()[2]
  now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  if mem_percent > self.up:
   filename = 'mem.txt'
   with open(filename, 'w') as f: # 如果filename不存在會(huì)自動(dòng)創(chuàng)建, 'w'表示寫數(shù)據(jù),寫之前會(huì)清空文件中的原有數(shù)據(jù)!
    f.write(str(now) + "內(nèi)存使用率超過" + str(self.up) + "!!!!.\n")
    f.write(str(now) + "當(dāng)前內(nèi)存使用率" + str(mem_percent) + "!!!\n")
   mail = txtMail()
   try:
    mail.txt_send_mail(filename="test.config", alarm=filename)
    temp_msg = "內(nèi)存超標(biāo),當(dāng)前內(nèi)存使用率:" + str(mem_percent)
    logger1 = myloggers(temp_msg)
    logger1.maillogging()
    del mail
    gc.collect()
   except:
    print("文本文件格式不正確")

watchpc.py

# -*- coding: utf-8 -*-
# Author: WuYang
# Date-modified: 07Nov2019
# Function: send alarm data through SMTP protocol
# Architecture: cpu.py, mem.py, etc. is to monitor performance of server
# Lang: Python3.7
# Env: CentOS7/WindowServer 2012R2
import time
from cpu import mycpumonitor
from mem import mymemmonitor
import gc
if __name__ == "__main__":
 while True:
  memObj = mymemmonitor(50)
  memObj.mem_monitor()
  cpuObj = mycpumonitor(10)
  cpuObj.cpu_monitor()
  del memObj
  gc.collect()
  time.sleep(10)

emailsender.py

#-*- coding: utf-8 -*-
import smtplib
import chardet
import codecs
import os
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
# 第三方SMTP服務(wù)
class txtMail(object):
 def __init__(self, host=None, auth_user=None, auth_password=None):
  self.host = "smtp.163.com" if host is None else host # 設(shè)置發(fā)送郵件服務(wù)使用專用報(bào)警賬戶的用戶名
  self.auth_user = "xxxxxx@163.com" if auth_user is None else auth_user # 上線時(shí)使用專用報(bào)警賬戶的用戶名
  self.auth_password = (
   "xxxxxxxx" if auth_password is None else auth_password
  ) # 上線時(shí)使用專用報(bào)警賬戶的密碼
  self.sender = "xxxxxxxx@163.com"
 def send_mail(self, subject, msg_str, recipient_list, attachment_list=None):
  message = MIMEMultipart()
  message["From"] = self.sender
  message["To"] = Header(";".join(recipient_list), "utf-8")
  message["Subject"] = Header(subject, "utf-8")
  message.attach(MIMEText(msg_str, "plain", "utf-8"))
  # 如果有附件,則添加附件
  if attachment_list:
   for att in attachment_list:
    attachment = MIMEText(open(att, "rb").read(), "base64", "utf-8")
    attachment["Content-Type"] = "application/octet-stream"
    # 這里filename可以任意寫,寫什么名字,郵件中顯示什么名字
    filename = os.path.basename(att)
    attachment.add_header(
     "Content-Disposition",
     "attachment",
     filename=("utf-8", "", filename),
    )
    message.attach(attachment)
  smtpObj = smtplib.SMTP_SSL(self.host)
  smtpObj.connect(self.host, smtplib.SMTP_SSL_PORT)
  smtpObj.login(self.auth_user, self.auth_password)
  smtpObj.sendmail(self.sender, recipient_list, message.as_string())
  smtpObj.quit()
  print("郵件發(fā)送成功")
  print(attachment_list)
 def guess_chardet(self, filename):
  """
  :param filename: 傳入一個(gè)文本文件
  :return: 返回文本文件的編碼格式
  """
  encoding = None
  try:
   # 由于本需求所解析的文本文件都不大,可以一次性讀入內(nèi)存
   # 如果是大文件,則讀取固定字節(jié)數(shù)
   raw = open(filename, "rb").read()
   if raw.startswith(codecs.BOM_UTF8): # 處理UTF-8 with BOM
    encoding = "utf-8-sig"
   else:
    result = chardet.detect(raw)
    encoding = result["encoding"]
  except:
   pass
  return encoding
 def txt_send_mail(self, filename, alarm):
  """
  :param filename:
  :return:
  將指定格式的txt文件發(fā)送至郵箱,txt文件樣例如下
  # 收件人,逗號(hào)分隔
  # 附件,逗號(hào)分隔
  """
  with open(filename, encoding=self.guess_chardet(filename)) as f:
   lines = f.readlines()
  recipient_list = lines[0].strip().split(",")
  attachment_list = []
  for file in lines[-1].strip().split(","):
   if os.path.isfile(file):
    attachment_list.append(file)
  # 如果沒有附件,則為None
  if attachment_list == []:
   attachment_list = None
  with open(alarm, encoding=self.guess_chardet(alarm)) as f1:
   lines1 = f1.readlines()
  subject = lines1[0].strip()
  msg_str = "".join(lines1[1:])
  self.send_mail(
   subject=subject,
   msg_str=msg_str,
   recipient_list=recipient_list,
   attachment_list=attachment_list,
  )

test.config

XXXXX@qq.com,XXXXX@163.com
libvirt.log,qemu.log

注意事項(xiàng)

需進(jìn)入郵箱設(shè)置,開啟POP3/SMTP/IMAP。

總結(jié)

以上所述是小編給大家介紹的Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 一文帶你了解Python中的枚舉(enum)

    一文帶你了解Python中的枚舉(enum)

    這篇文章一文帶你了解Python中的枚舉(enum),在Python中,枚舉和我們?cè)趯?duì)象中定義的類變量時(shí)一樣的,每一個(gè)類變量就是一個(gè)枚舉項(xiàng),需要的朋友可以參考下
    2023-04-04
  • 基于Python正確讀取資源文件

    基于Python正確讀取資源文件

    這篇文章主要介紹了基于Python正確讀取資源文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python中退出多層循環(huán)的方法

    python中退出多層循環(huán)的方法

    這篇文章主要介紹了python中退出多層循環(huán)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • python實(shí)現(xiàn)圖書館借閱系統(tǒng)

    python實(shí)現(xiàn)圖書館借閱系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖書館借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python學(xué)習(xí)筆記之文件的讀寫操作實(shí)例分析

    Python學(xué)習(xí)筆記之文件的讀寫操作實(shí)例分析

    這篇文章主要介紹了Python學(xué)習(xí)筆記之文件的讀寫操作,結(jié)合實(shí)例形式詳細(xì)分析了Python常見的文件讀寫操作實(shí)現(xiàn)技巧及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • 使用python編寫android截屏腳本雙擊運(yùn)行即可

    使用python編寫android截屏腳本雙擊運(yùn)行即可

    使用python編寫一個(gè)截屏的腳本,雙擊運(yùn)行腳本就OK,截屏成功后會(huì)將截屏文件已當(dāng)前時(shí)間命名,并保存在存放腳本的當(dāng)前路徑的screenshot文件夾下
    2014-07-07
  • Python創(chuàng)建或生成列表的操作方法

    Python創(chuàng)建或生成列表的操作方法

    在本文中我們給大家分享了關(guān)于Python創(chuàng)建或生成列表的操作方法以及步驟圖文流程,需要的朋友們學(xué)習(xí)下。
    2019-06-06
  • Python模塊學(xué)習(xí)之subprocess詳解

    Python模塊學(xué)習(xí)之subprocess詳解

    subprocess是Python?2.4中新增的一個(gè)模塊,它允許你生成新的進(jìn)程,連接到它們的?input/output/error?管道,并獲取它們的返回(狀態(tài))碼,下面小編就來和大家聊聊它的具體使用吧
    2023-08-08
  • 詳解Python函數(shù)式編程—高階函數(shù)

    詳解Python函數(shù)式編程—高階函數(shù)

    這篇文章主要介紹了Python函數(shù)式編程—高階函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 解決keras加入lambda層時(shí)shape的問題

    解決keras加入lambda層時(shí)shape的問題

    這篇文章主要介紹了解決keras加入lambda層時(shí)shape的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評(píng)論