#!/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)
05-28 21:57