标签:监控网站
前面已经写过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
标签:监控网站
原文地址:http://hbbdgyb.blog.51cto.com/5031387/1953068