关闭菜单
四少爷的blog
by Jermey
V 1.0
打开菜单

python3监控网站状态

直接上代码::

import configparser,requests
from time import sleep
import datetime
 
class checkurl(object):
    def __init__(self,file):
        self.file=file
        self.cfg=configparser.ConfigParser()
         
    def cfg_load(self):
        self.cfg.read(self.file)
        self.allurl=self.cfg.items('yuming')
        self.reload=self.cfg.get('time','reload')
         
    def sendmessage(self,errinfo):
        print("异常通知代码")
        # 需自行实现异常通知功能
             
    def senderror(self,errcont):
        enow=datetime.datetime.now()
        now=enow.strftime('%Y-%m-%d %H:%M:%S')
        errfile=open('url.log','a')
        errfile.write(now)
        errfile.write(str(errcont))
        errfile.write('\n')
        errfile.close()
             
    def cfg_dump(self):
        while True:
            for k,v in self.allurl:
                checknum=0
                #设置重试错误次数
                while checknum < 5:
                    try:
                        res=requests.get(v,timeout=20)
                        print(v,res.status_code)
                        res.close()
                        if res.status_code >= 400:
                            errinfo=v+' '+str(res.status_code)
                            self.sendmessage(errinfo)
                            self.senderror(errinfo)
                        break
                    except:
                        errinfo=v+' is error'
                        print(errinfo+'\r\n请稍等,正在第',checknum+1,'次重试...')
                        sleep(1)
                        if checknum == 4:
                            print('重试仍然无法连接,正在发送微信和邮件报警...')
                            self.sendmessage(errinfo)
                            self.senderror(errinfo)
                    checknum=checknum+1
            print('-----------------------------------')
            nextcheck=0
            while nextcheck < int(self.reload):
                print('距离下次检测还剩',int(self.reload)-nextcheck,'秒')
                sleep(1)
                nextcheck=nextcheck+1
 
if __name__ =='__main__':
    check=checkurl('yuming.ini')
    check.cfg_load()
    check.cfg_dump()

配置文件:

[yuming]
yuming1=https://www.baidu.com
yuming2=http://www.qq.com
yuming3=http://www.163.com
 
[time]
reload=60

(完)

文章抄自:郭亚彬