监控域名是否可访问,不可访问时微信通知

文章讲述了如何利用Python的requests库进行网页抓取,并结合scheduler和threading实现定时任务,当域名状态异常时发送告警通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值