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

基于Python腳本實現(xiàn)郵件報警功能

 更新時間:2020年05月20日 08:37:12   作者:Mr_Wmn  
這篇文章主要介紹了基于Python腳本實現(xiàn)郵件報警功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

使用了smtplib等第三方庫,進行發(fā)送郵件,完成郵件報警功能

如下是實例 :

#!/usr/bin/python
 
import glob
import operator
from optparse import OptionParser
import smtplib
import email.MIMEText as MIMEText
import email.Utils.formadate as formatdate
 
msg = ""
#主方法
def main():
  global options
  global msg
 
  parser = OptionParser(add_help_option=False)
  parser.add_option("-m", "--mail", dest="mail", type="str", help="email address to send report result (comma separated)")
  parser.add_option("-t", "--title", dest="title", type="str", help="email title (default:Error File Count)")
  parser.add_option("-a", "--admin", dest="admin", type="str", help="set sender address. works with -m option")
  (options, args) = parser.parse_args()
 
  #這里監(jiān)控文件夾下的文件數(shù),超出25個文件即報警
  datanum = cntFiles("/data/","csv")
  if (operator.gt(datanum,25)):
    msg += " Please be alert : \n the number of files under /data/ path is greater than 25 :"
    msg += "\n =========================================="
    msg += "\n The number of files is : " + str(datanum)
    sendmsg(options,msg)
  print("==== end ====")
 
#添加發(fā)送郵件的信息
def sendmsg(options,msg):
  if options.mail:
    toAddr = options.mail
    if options.admin:
      fromAddr = options.admin
    else:
      fromAddr = 'zhangsan@neiyou.cn'#這里是公司的公用SMTP郵箱賬號
 
    if options.title:
      subject = options.title
    else:
      subject = 'File Stacking Alarm'
    msg += "\n ========================================== \n"
    print( msg)
    msg = createMsg(fromAddr, toAddr, subject, msg)
    print( msg)
    send(fromAddr, toAddr, msg)
  else:
    print( msg)
 
#glob方法,統(tǒng)計文件夾下的文件數(shù)
def cntFiles(in_directory, ext):
  stage = len(glob.glob1(in_directory,"*." + ext))
  return stage
 
#創(chuàng)建郵件頭
def createMsg(fromAddr, toAddr, subject, body):
  msg = MIMEText(body)
  msg['Subject'] = subject
  msg['To'] = toAddr
  msg['From'] = fromAddr
  msg['Date'] = formatdate()
  return msg
 
#發(fā)送郵件
def send(fromAddr, toAddr, msg):
  try:
    #這里添加公司的SMTP郵箱地址
     s = smtplib.SMTP('192.168.12.120')
     s.sendmail(fromAddr, toAddr.split(','), msg.as_string())
     s.close()
     print("SUCCESS: sending email")
  except smtplib.SMTPException:
     print("ERROR: sending email")
 
if __name__ == '__main__':
  main()

linux上做計劃任務(wù),把指令添加進計劃任務(wù)中:

Errymsfileemail.py -m zhangsan@gongsi.cn -t "[ERROR/$HOST] File Stacking Alarm"

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫

    Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫

    這篇文章主要介紹了Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • python閉包和裝飾器你了解嗎

    python閉包和裝飾器你了解嗎

    這篇文章主要為大家詳細(xì)介紹了Python的閉包和裝飾器,何為閉包?何為裝飾器?感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-08-08
  • 淺談Tensorflow由于版本問題出現(xiàn)的幾種錯誤及解決方法

    淺談Tensorflow由于版本問題出現(xiàn)的幾種錯誤及解決方法

    今天小編就為大家分享一篇淺談Tensorflow由于版本問題出現(xiàn)的幾種錯誤及解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python遞歸函數(shù)繪制分形樹的方法

    python遞歸函數(shù)繪制分形樹的方法

    這篇文章主要為大家詳細(xì)介紹了python遞歸函數(shù)繪制分形樹的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 一文學(xué)會Python列表list的使用

    一文學(xué)會Python列表list的使用

    這篇文章主要為大家介紹了Python列表list的使用全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Python函數(shù)和模塊的使用詳情

    Python函數(shù)和模塊的使用詳情

    這篇文章主要介紹了Python函數(shù)和模塊的使用詳情,文章基于python的相關(guān)資料展開主題的具體介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • python3之讀取redis數(shù)據(jù)帶有‘b’的問題

    python3之讀取redis數(shù)據(jù)帶有‘b’的問題

    這篇文章主要介紹了python3之讀取redis數(shù)據(jù)帶有‘b’的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 數(shù)據(jù)清洗--DataFrame中的空值處理方法

    數(shù)據(jù)清洗--DataFrame中的空值處理方法

    今天小編就為大家分享一篇數(shù)據(jù)清洗--DataFrame中的空值處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 使用Pycharm分段執(zhí)行代碼

    使用Pycharm分段執(zhí)行代碼

    這篇文章主要介紹了使用Pycharm分段執(zhí)行代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • pyinstaller打包成無控制臺程序時運行出錯(與popen沖突的解決方法)

    pyinstaller打包成無控制臺程序時運行出錯(與popen沖突的解決方法)

    這篇文章主要介紹了pyinstaller打包成無控制臺程序時運行出錯(與popen沖突的解決方法),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評論