python3 网站状态监控_python3监控网站状态

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

import configparser,requests

from time import sleep

import WeChat,Mail,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')

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 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__':

mail=Mail.sendmail()

wechat=WeChat.WeChat()

check=checkurl('yuming.ini')

check.cfg_load()

check.cfg_dump()

下面是yuming.ini的配置

[yuming]

yuming1=https://www.baidu.com

yuming2=http://www.qq.com

yuming3=http://www.163.com

[time]

reload=60

[mailto]

mail1=guoyabin@ccln.gov.cn

mail2=guoyabin@bj1921.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值