首页 > 编程语言 > 详细

python3监控网站状态

时间:2017-08-02 21:54:27      阅读:269      评论:0      收藏:0      [点我收藏+]

前面已经写过Python3发邮件,Python发微信的文章了。直接导入即可。

#!/usr/local/env python
import configparser,requests
from time import sleep
import WeChat,Mail
 
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‘)
        self.mailto=self.cfg.items(‘mailto‘)
         
    def sendmessage(self,errinfo):
        wechat.send(‘@all‘,errinfo)
        for key,values in self.mailto:
            mail.send(values,errinfo,‘url访问失败报警‘)
             
    def cfg_dump(self):
        while True:
            for k,v in self.allurl:
                try:
                    res=requests.get(v)
                    print(v,res.status_code)
                    res.close()
                    if res.status_code >= 400:
                        errinfo=v+‘ ‘+str(res.status_code)
                        self.sendmessage(errinfo)
                except:
                    errinfo=v+‘ is error‘
                    print(errinfo+‘\r\n请稍等正在重试...‘)
                    sleep(1)
                    try:
                        res=requests.get(v)
                        res.close()
                    except:
                        print(‘重试仍然无法连接,正在发送微信和邮件报警...‘)
                        self.sendmessage(errinfo)
            sleep(int(self.reload))
 
if __name__ ==‘__main__‘:
    mail=Mail.sendmail()
    wechat=WeChat.WeChat()
    check=checkurl(‘yuming.ini‘)
    check.cfg_load()
    check.cfg_dump()


本文出自 “hbbdgyb” 博客,请务必保留此出处http://hbbdgyb.blog.51cto.com/5031387/1953068

python3监控网站状态

原文:http://hbbdgyb.blog.51cto.com/5031387/1953068

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!