zoukankan      html  css  js  c++  java
  • 【原】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)
    
    
    
    
  • 相关阅读:
    java继承
    java构造器
    java接口
    java 泛型详解---转载
    java竞争抢答器
    java并发资源访问_01
    java多线程数字加减
    java多线程计算机流水线模型
    Java并发编程:Callable、Future和FutureTask---转载测试
    购物车
  • 原文地址:https://www.cnblogs.com/liyongjian5179/p/11651369.html
Copyright © 2011-2022 走看看