原python 检查网站访问是否超时,并用钉钉机器人报警
Posted liyongjian5179
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原python 检查网站访问是否超时,并用钉钉机器人报警相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
import requests
import json
import logging
webhook="上面创建钉钉机器人的webhook地址"
logfile='C:\Users\lyj\Desktop\lyj.txt'
urls = [
'http://www.baidu.com',
'http://www.sohu.com',
'http://www.sina.com',
'http://www.google.com.hk'
]
def check_url_state(url,timeout=5):
try:
r = requests.get(url, timeout=timeout)
return r.status_code
except requests.exceptions.RequestException as e:
#print(e)
logging.error(e)
return "timeout"
def send_ding(text):
json_data={
"msgtype": "text",
"text": {
"content": text
}
}
print(json_data)
headers = {'Content-Type': 'application/json'}
#x=requests.post(url=webhook,data=json.dumps(json_data),headers=headers)
logging.info(json_data)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s',
#datefmt='%Y-%m-%d %H:%M:%S %p', # 时间
filename=logfile,
filemode='a')
for url in urls:
if check_url_state(url,1) == "timeout":
print("报警:",url)
content = '报警网站:{}'.format(url)
send_ding(content)
logging.warning(content)
else:
print("正常:",url)
logging.info('正常: %s',url)
以上是关于原python 检查网站访问是否超时,并用钉钉机器人报警的主要内容,如果未能解决你的问题,请参考以下文章