有些VPS或者独立服务器的性价比很高,基本上供不应求,这个时候,仅仅可以靠人力去刷新页面是很难买到的。而一个定时运行的脚本则可以替我们实现这个功能。于是写了一个python3的监控程序,配合server酱可以实现实时微信推送。
程序放在Github上,点击这里查看,程序名称为oos.py,取Out of Stock之意。

# !/usr/bin/env python
# -*- coding: UTF-8 -*-

from urllib import request
import requests
from datetime import datetime

timeout = 10
time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
key = 'SCKEY'
title = "上货监测"
url = ""
head = {
    'Connection': 'Keep-Alive',
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}

try:
    req = request.Request(url, headers=head)
    response = request.urlopen(req, timeout=timeout)
    html = response.read()
    html = html.decode("utf-8").lower()
    str="out of stock"
    if str in html:
        print("缺货" + "\n" + time)
    else:
        print("不缺货" + "\n" + time)
        content = "上货啦~" + "\n" + time
        payload = {
            'text': title,
            'desp':content
        }
        fturl = 'https://sc.ftqq.com/{}.send'.format(key)
        requests.post(fturl, params=payload, timeout=timeout)
except Exception:
    print("Error" + "\n" + time)

其中的url为VPS/独立服务器的购买页面;SCKEY为server酱的密钥,可以点击这里免费申请一个;str="out of stock"这一行为页面关键字监控,可以自行更改,一般都是out of stock;配合crontab可以实现10秒级别的上货查询!

标签: 教程, linux, 监控

已有 10 条评论

  1. 大佬不错哈哈哈喜欢

  2. 报错,,,
    Traceback (most recent call last):
    File "oos.py", line 4, in
    from urllib import Request
    ImportError: cannot import name Request

    1. 缺少urllib模块,如果系统没有自带的话,去https://pkgs.org/download/python-urllib3查看对应系统版本的包,安装后即可正常使用。

  3. nick nick

    我这边一直显示Error不知道为什么

    1. 有其他详细的报错信息吗?

  4. 123123 123123

    太椴了,我喜欢

  5. qwer121 qwer121

    [root@server ~]# bash oos.py
    oos.py: line 4: from: command not found
    oos.py: line 5: import: command not found
    oos.py: line 6: from: command not found
    timeout: invalid time interval '='
    Try 'timeout --help' for more information.
    oos.py: line 9: syntax error near unexpected token `('
    oos.py: line 9: `time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')'

    可能是用的方法不对??

    1. qwer121 qwer121

      我错了,应该是这样的。。
      [root@server ~]# python oos.py
      Traceback (most recent call last):
      File "oos.py", line 4, in
      from urllib import request
      ImportError: cannot import name request
      缺少urllib模块 懂了。

      1. qwer121 qwer121

        root@duomi:~# apt-get install python-urllib3
        Reading package lists... Done
        Building dependency tree
        Reading state information... Done
        python-urllib3 is already the newest version (1.19.1-1).
        0 upgraded, 0 newly installed, 0 to remove and 86 not upgraded.

        应该是显示已经安装了吧??但是还是不行啊。提示错误如上。。

  6. qwer121 qwer121

    这个urllib到底如何安装。。求dalao指点。。

添加新评论