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)
    
    
    
    
  • 相关阅读:
    stm32的hal之串口库函数总结复习
    关闭win10 任务栏窗口预览的步骤:
    sizeof的注意点
    goto语句——慎用,但是可以用
    #define的一个小技巧
    Chapter 1 First Sight——36
    Chapter 1 First Sight——35
    Chapter 1 First Sight——34
    leetcode409
    Chapter 1 First Sight——34
  • 原文地址:https://www.cnblogs.com/liyongjian5179/p/11651369.html
Copyright © 2011-2022 走看看