import requests
import sched
import time
# 创建 scheduler 实例
scheduler = sched.scheduler(time.time, time.sleep)
def send_wechat(msg):
token = 'xxxxxxxxxxxx' # 前边复制到那个token
title = '域名异常告警'
content = msg
template = 'html'
url = f"https://www.pushplus.plus/send?token={token}&title={title}&content={content}&template={template}"
print(url)
r = requests.get(url=url)
print(r.text)
import threading
import time
def repeat_task(interval, task):
def function():
while True:
task()
time.sleep(interval)
thread = threading.Thread(target=function)
thread.start()
def my_task():
print("任务执行:", time.ctime())
url = 'http://baiduu.com/' # 替换为你想访问的域名
try:
response = requests.get(url)
if response.status_code == 200:
print(response.text) # 打印响应的文本内容
else:
send_wechat("域名状态异常")
print(f"Failed to retrieve the webpage: {response.status_code}")
except Exception as e:
print('Exception'+str(e)) # 打印响应的文本内容
send_wechat("域名状态异常")
# 设置任务每5秒执行一次
repeat_task(5, my_task)
监控域名是否可访问,不可访问时微信通知
最新推荐文章于 2024-07-21 22:27:18 发布
文章讲述了如何利用Python的requests库进行网页抓取,并结合scheduler和threading实现定时任务,当域名状态异常时发送告警通知。
摘要由CSDN通过智能技术生成