最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python自動(dòng)監(jiān)控網(wǎng)站并發(fā)送郵件告警的方法

 更新時(shí)間:2016年08月24日 09:52:22   投稿:daisy  
這篇文章介紹的是通過(guò)定時(shí)執(zhí)行python腳本,可以實(shí)現(xiàn)定期批量訪問(wèn)網(wǎng)站,如果發(fā)現(xiàn)網(wǎng)站打不開(kāi),第一時(shí)間發(fā)郵件到管理員郵箱進(jìn)行預(yù)警。有需要的可以參考借鑒。

前言

因?yàn)橛幸恍┚W(wǎng)站需要每日檢查是否有問(wèn)題,所以需要一個(gè)報(bào)警監(jiān)控的機(jī)制,這個(gè)需要你指定你發(fā)送的郵箱和你接收的郵箱,就可以做到對(duì)網(wǎng)站自動(dòng)監(jiān)控了。

這里用的是python3.5

需要安裝的插件:

      1、smtplib:發(fā)郵件需要用到

      2、pycurl:訪問(wèn)網(wǎng)站時(shí)會(huì)需要用到

      3、linecache:在讀取txt網(wǎng)站清單時(shí)需要用到

具體思路:

python程序從txt里面批量讀取到網(wǎng)站的信息,通過(guò)Curl.py模擬瀏覽器去訪問(wèn)網(wǎng)站,并且把訪問(wèn)的結(jié)果寫(xiě)入到以自己的網(wǎng)站名稱(chēng)-日期.txt格式的文件中記錄;有幾種情況:

1、如果發(fā)現(xiàn)打不開(kāi)了,直接發(fā)郵件提示網(wǎng)站已經(jīng)打不開(kāi)

2、發(fā)現(xiàn)可以打開(kāi),讀取文件中上一次訪問(wèn)的情況(讀取txt文件最后一行),

    1)如果發(fā)現(xiàn)上一次是打不開(kāi)的,發(fā)郵件提醒網(wǎng)站已經(jīng)恢復(fù)了

    2)如果發(fā)現(xiàn)上一次是打得開(kāi)的(200的返回碼),只是記錄網(wǎng)站訪問(wèn)的日志就可以了

總共4個(gè)文件

Email.py是郵件類(lèi),主要用來(lái)發(fā)郵件的時(shí)候調(diào)用,這里需要按照你的情況改成你的郵箱(msg['From']),郵箱服務(wù)器地址(SMTP地址),和你的郵箱密碼(SMTP.login)

Email.py

#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class Email_send(object):
 def __init__(self,msgTo,data2,Subject):
  self.msgTo=msgTo
  self.data2=data2
  self.Subject=Subject
 def sendEmail(self):
  # (attachment,html) = content
  msg = MIMEMultipart()
  msg['Subject'] = self.Subject
  msg['From'] = 'xxxx@xxxx.com.cn'
  msg['To'] = self.msgTo
  html_att = MIMEText(self.data2, 'html', 'utf-8')
  #att = MIMEText(attachment, 'plain', 'utf-8')
  msg.attach(html_att)
  #msg.attach(att)
  try:
   smtp = smtplib.SMTP()
   smtp.connect('smtp.xxxx.com', 25)
   smtp.login(msg['From'], 'xxxx') #改成自己的郵箱密碼
   smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string())
   return('郵件發(fā)送成功')
  except Exception as e:
   print('--------------sss------',e)
 def curl(self):
  import pycurl
  c=pycurl.Curl()
  #url="www.luoan.com.cn"
  #indexfile=open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")
  c.setopt(c.URL,url)
  c.setopt(c.VERBOSE,1)
  c.setopt(c.ENCODING,"gzip")
  #模擬火狐瀏覽器
  c.setopt(c.USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0")
  return c

Curl.py 主要用來(lái)執(zhí)行模擬瀏覽器訪問(wèn)網(wǎng)站并返回結(jié)果的文件

#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
import pycurl
class Curl(object):
 def __init__(self,url):
  self.url=url
 def Curl_site(self):
  c=pycurl.Curl()
  #url="www.luoan.com.cn"
  #indexfile=open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")
  c.setopt(c.URL,self.url)
  c.setopt(c.VERBOSE,1)
  c.setopt(c.ENCODING,"gzip")
  #模擬火狐瀏覽器
  c.setopt(c.USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0")
  return c

site_moniter.py 這個(gè)文件為主程序,主要執(zhí)行調(diào)用上面的函數(shù),讀取txt文件中的網(wǎng)站清單,如果網(wǎng)站打不開(kāi)就發(fā)郵件出來(lái)告警

需要注意:

      1、把xxxx@xxxx.com改成你自己的郵箱,

      2、把文件路徑改成自己的真實(shí)路徑

#!/usr/bin/python
#-*- coding:utf-8 -*-
import pycurl
import os
import sys
import linecache
import time #引入事件類(lèi),用來(lái)獲取系統(tǒng)當(dāng)前時(shí)間
#from ceshi import Student
from Email import Email_send
from Curl import Curl

#bart = Student('mafei',59)
#bart.print_score()

def script(urls,type):
 msgTo = 'xxxx@xxxx.com'
 now_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
 j=1
#  data2=[{'aa':'aa'}]
 for url_split in urls:
  #print(url_split)
  url_1=url_split.split('---')
  url=url_1[1]
  recovery_title = "監(jiān)控通知----%s url:%s" % (url_1[0], url) + "在" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) + "已經(jīng)恢復(fù)"
  down_title = "監(jiān)控通知----%s url:%s" % (url_1[0], url) + "在" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) + "無(wú)法打開(kāi)"
  #print('~~~~~~~~~~~~~~~~~~~')
  #print(url)
  #引用爬去網(wǎng)站的類(lèi),調(diào)用結(jié)果
  url_result = Curl(url)
  c = url_result.Curl_site()
  try:
   c.perform()
   code = str(c.getinfo(c.HTTP_CODE))
   print(code+'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
  except Exception as e:
   print('--------錯(cuò)誤信息:--------',e)
   #indexfile.close()
   #c.close()
  code = str(c.getinfo(c.HTTP_CODE))
  # print(code+'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
  filename = '%s-%s.txt' % (url_1[0], time.strftime("%Y-%m-%d", time.localtime(time.time())))
  #判斷如果在網(wǎng)站無(wú)法打開(kāi)的情況下
  if code == '0' or code=='400' or code=='500' or code=='404':
   resolveTime = 0
   Connection_Time = 0
   Transfer_Total_Time = 0
   Total_Time = 0
   # print('為000000000000000000000000000000000000000000')
   data3 = '網(wǎng)站:%s無(wú)法打開(kāi)%s' % (url_1[0], url)
   # indexfile.close()
   # c.close()
   #判斷網(wǎng)站如果掛了就發(fā)郵件
   stat3 = Email_send(msgTo, data3, down_title)
   resole=stat3.sendEmail()
   print(resole)
   print(data3 + '郵件已經(jīng)發(fā)送')
  else:
   #resolveTime = str(c.getinfo(c.NAMELOOKUP_TIME) * 1000) + " ms"
   # Connection_Time=str(float(c.getinfo(c.CONNECT_TIME)*1000-c.getinfo(c.NAMELOOKUP_TIME)*1000))+" ms"
   #Connection_Time = str(c.getinfo(c.CONNECT_TIME) * 1000 - c.getinfo(c.NAMELOOKUP_TIME) * 1000) + " ms"
   # Connection_Time=round(float(Connection_Time))
   #Transfer_Total_Time = str(c.getinfo(c.TOTAL_TIME) * 1000 - c.getinfo(c.PRETRANSFER_TIME) * 1000) + " ms"
   #Total_Time = str(c.getinfo(c.TOTAL_TIME) * 1000) + " ms"
   # data2=data
   # data={'url':url,'HTTP CODE':code,'resolveTime':resolveTime,'Connection_Time':Connection_Time,'Transfer_Total_Time':Transfer_Total_Time,'Total_Time':Total_Time}
   print('網(wǎng)站可以正常打開(kāi)')
   #f = open(filename, 'a',encoding='utf-8')
   file_exit=os.path.exists(filename)
   #print(file_exit)
   #判斷這個(gè)日志文件存不存在
   if(file_exit):
    #讀取文件最后一行,為了讀取出來(lái)最后一次的狀態(tài)值
    file = open(filename, 'r',encoding='utf-8')
    linecount = len(file.readlines())
    data = linecache.getline(filename, linecount)
    file.close
    if data == '':
     print('這是'+data+'為空的數(shù)據(jù)')
    else:
     print('其他信息%s'%(data))
     explode = data.split('----')
     #判斷如果讀取出來(lái)的值,最后一次是異常的情況就告警
     if explode[3]=='0\n' or explode[3]=='400\n' or explode[3]=='500' or explode[3]=='404':
      data3 = '網(wǎng)站:%s在%s已經(jīng)恢復(fù)%s' % (url_1[0], now_time,url)
      stat3 = Email_send(msgTo, data3, recovery_title)
      resole = stat3.sendEmail()
      print(resole)
      print(data3 + '郵件已經(jīng)發(fā)送')
     else:
      print('最后一次記錄為其他值:%s'%(explode[3])+'-----')
   else:
    print('文件不存在')
  data2 = '\n' + url_1[0] + '----' + url + '-----' + time.strftime("%H:%M:%S", time.localtime(time.time())) + '-------' + code
  print('data2數(shù)據(jù)寫(xiě)入成功:' + data2)
  file = open(filename, 'a', encoding='utf-8')
  file.write(data2)
  file.close
# bart = Student(data2,59)
# bart.print_score()

if __name__ == "__main__":
 type = "監(jiān)控通知-測(cè)試" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
 data1=['公司門(mén)戶---www.luoan.com.cn','公司平臺(tái)---yun.luoan.com.cn']
 #script(data1,type)


 #中心層面的網(wǎng)站清單
 file=open('D:\python\site_moniter\zhongxin.txt')
 data2=[]
 while 1:
  line2 =file.readline()
  print(line2)
  if not line2:
   break
  data2.append(line2[0:-1])
 #data2=['www.luoan.com.cn','yun.luoan.com.cn','www.qq.com']
 print(data2)
 title="監(jiān)控通知-中心"+ time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
 script(data2,title)

總結(jié)

利用Python自動(dòng)監(jiān)控網(wǎng)站并發(fā)送郵件告警的方法到這就基本結(jié)束了,希望對(duì)大家的學(xué)習(xí)工作能有所幫助。

相關(guān)文章

  • 利用python做表格數(shù)據(jù)處理

    利用python做表格數(shù)據(jù)處理

    這篇文章主要介紹了如何利用python做表格數(shù)據(jù)處理,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • 解決python使用pd.read_csv()出現(xiàn)錯(cuò)誤UnicodeDecodeError:?'utf-8'?codec?can't?decode......

    解決python使用pd.read_csv()出現(xiàn)錯(cuò)誤UnicodeDecodeError:?'utf-8&

    你是否有過(guò)之前用pd.read打開(kāi)csv文件都正常,但突然有一天運(yùn)行以前的代碼就突然報(bào)錯(cuò),這篇文章主要給大家介紹了關(guān)于如何解決python使用pd.read_csv()出現(xiàn)錯(cuò)誤UnicodeDecodeError:?'utf-8'?codec?can't?decode......的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • 如何使用python把ppt轉(zhuǎn)換成pdf

    如何使用python把ppt轉(zhuǎn)換成pdf

    這篇文章主要介紹了如何使用python把ppt轉(zhuǎn)換成pdf,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 分析Python list操作為什么會(huì)錯(cuò)誤

    分析Python list操作為什么會(huì)錯(cuò)誤

    這篇文章主要介紹了分析Python list操作為什么會(huì)錯(cuò)誤,python搞數(shù)據(jù)分析,在很多方面python有著比Matlab更大的優(yōu)勢(shì),下面來(lái)看看文章具體介紹的相關(guān)內(nèi)容吧,需要的朋友可以參考一下
    2021-11-11
  • OpenCV-Python實(shí)現(xiàn)通用形態(tài)學(xué)函數(shù)

    OpenCV-Python實(shí)現(xiàn)通用形態(tài)學(xué)函數(shù)

    本文將結(jié)合實(shí)例代碼,介紹OpenCV-Python實(shí)現(xiàn)通用形態(tài)學(xué)函數(shù),包含開(kāi)運(yùn)算,閉運(yùn)算等復(fù)雜的形態(tài)學(xué)運(yùn)算,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Pandas替換NaN值的方法實(shí)現(xiàn)

    Pandas替換NaN值的方法實(shí)現(xiàn)

    本文主要介紹了Pandas替換NaN值的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Pandas中如何對(duì)DataFrame列名進(jìn)行重命名

    Pandas中如何對(duì)DataFrame列名進(jìn)行重命名

    在做數(shù)據(jù)挖掘的時(shí)候,想改一個(gè)DataFrame的column名稱(chēng),所以就查了一下,下面這篇文章主要給大家介紹了關(guān)于Pandas中如何對(duì)DataFrame列名進(jìn)行重命名的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • 用Python輸出一個(gè)楊輝三角的例子

    用Python輸出一個(gè)楊輝三角的例子

    這篇文章主要介紹了用Python和erlang輸出一個(gè)楊輝三角的例子,同時(shí)還提供了一個(gè)erlang版楊輝三角,需要的朋友可以參考下
    2014-06-06
  • python語(yǔ)音識(shí)別指南終極版(有這一篇足矣)

    python語(yǔ)音識(shí)別指南終極版(有這一篇足矣)

    這篇文章主要介紹了python語(yǔ)音識(shí)別指南終極版的相關(guān)資料,包括語(yǔ)音識(shí)別的工作原理及使用代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python torch.utils.data.DataLoader使用方法

    python torch.utils.data.DataLoader使用方法

    這篇文章主要介紹了python torch.utils.data.DataLoader使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論

诸城市| 霞浦县| 绥滨县| 克拉玛依市| 保德县| 盘锦市| 凤阳县| 乐安县| 罗定市| 宜良县| 封开县| 上饶市| 宁波市| 准格尔旗| 五寨县| 绥德县| 苍梧县| 云安县| 潍坊市| 玉田县| 苍溪县| 辽阳县| 昭觉县| 桦甸市| 通江县| 成安县| 桑植县| 石城县| 桑日县| 乌拉特前旗| 江达县| 陵川县| 仪征市| 慈利县| 务川| 峨眉山市| 于都县| 夹江县| 佛学| 海林市| 阜阳市|