import requests import time import json requests.packages.urllib3.disable_warnings() # 当前时间 格式化 方法 def get_current_time(): current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) return current_time def check_website(): url = "https://app.yizhizs.cn/api/card-bag/findPublicPage?outTags=&pageNum=1&pageSize=20" try: response = requests.get(url, verify=False) if response.json()['code'] == 200: print("网站正常运行:" + get_current_time()) else: print("网站异常,正在重试" + get_current_time()) send_alert() except requests.exceptions.RequestException as e: print("网站异常,正在重试" + get_current_time()) send_alert() def send_alert(): alert_url = "https://www.feishu.cn/flow/api/trigger-webhook/ee26be1f55031f2debd664cbd377a3b8" payload = { "title": "首页访问异常", "content": "首页访问异常" } headers = { "Content-Type": "application/json" } try: response = requests.post(alert_url, headers=headers, data=json.dumps(payload)) if response.status_code == 200: print("报警发送成功" + get_current_time()) else: print("报警发送失败" + get_current_time()) except requests.exceptions.RequestException as e: print("报警发送失败" + get_current_time()) if __name__ == "__main__": while True: check_website() time.sleep(20)