码迷,mamicode.com
首页 > 编程语言 > 详细

【原】python 检查网站访问是否超时,并用钉钉机器人报警

时间:2019-10-11 01:12:26      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:form   创建   headers   asi   check   evel   message   warning   date   

#!/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 检查网站访问是否超时,并用钉钉机器人报警

标签:form   创建   headers   asi   check   evel   message   warning   date   

原文地址:https://www.cnblogs.com/liyongjian5179/p/11651369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!