import requests
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def send_wechat(msg):
token = 'xxxxxxxxxxxx'
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("域名状态异常")
repeat_task(5, my_task)