前面已经写过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()
时间: 2024-11-03 21:38:46