python监控vps是否有货(钉钉报警提醒)

post by rocdk890 / 2020-10-20 16:22 Tuesday linux技术
最近一直在找稳定的vps(你们懂的),这样才能看遍全世界,最近看上了几个vps,但想买的时候没货,我又不想一直自己去刷网页关注,所以就写了个脚本来帮我监控着,为了方便接收到消息,我加入了钉钉来进行提醒.
系统:centos 7.x(64位)

cat /root/soft_shell/vps.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from urllib import request
import requests
import json
from datetime import datetime
from dingtalkchatbot.chatbot import DingtalkChatbot

time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
url_id = [74,75]
webhook = "https://oapi.dingtalk.com/robot/send?access_token=这里写你自己的key"
title = "上货监测"
header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
}

print('开始检查是否有货:')
try:
    flag=0
    while len(url_id) > 0 :
        pid=url_id.pop()
        url='https://manage.hostdare.com/cart.php?a=add&pid=%s' % (pid)
        head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
        req=request.Request(url,headers=head)
        page=request.urlopen(req).read()
        if str(page).find('out of stock')>0:
            flag=0
            print('无货')
        else:
            flag=flag+1
            print('有货')
            tex = "上货啦" + "\n" + url
            if flag<3:
                message={
                    "msgtype": "text",
                    "text": {
                        "content": tex
                        },
                        "at": {
                            "isAtAll": False
                        }
                    }
            message_json = json.dumps(message)
            requests.post(url=webhook,data=message_json,headers=header)

except:
    print('脚本异常,退出')

至于监控的频繁,大家可以根据自己的喜好去搞.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-775.html

标签: centos linux vps 监控 python python3 钉钉报警

  1. gravatar 绿软吧(lvr8)
    2020-10-27 01:57
    谢谢分享

评论: