自动检测公司网站是否正常访问

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import time

import requests

import json

from urllib.request import urlopen

from threading import Timer

class WeChat:

    def __init__(self):

        self.CORPID = 'wx0**********53046f'  #企业ID,在管理后台获取

        self.CORPSECRET = 'HtbfQASv4mkx_*****************Hn4qrn5_J6s'#自建应用的Secret,每个自建应用里都有单独的secret

        self.AGENTID = '100****'  #应用ID,在后台应用中获取

        self.TOUSER = "0100*****"  # 接收者用户名,多个用户用|分割

    def _get_access_token(self):

        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'

        values = {'corpid': self.CORPID,

                  'corpsecret': self.CORPSECRET,

                  }

        req = requests.post(url, params=values)

        data = json.loads(req.text)

        return data["access_token"]

    def get_access_token(self):

        try:

            with open('./tmp/access_token.conf', 'r') as f:

                t, access_token = f.read().split()

        except:

            with open('./tmp/access_token.conf', 'w') as f:

                access_token = self._get_access_token()

                cur_time = time.time()

                f.write('\t'.join([str(cur_time), access_token]))

                return access_token

        else:

            cur_time = time.time()

            if 0 < cur_time - float(t) < 7260:

                return access_token

            else:

                with open('./tmp/access_token.conf', 'w') as f:

                    access_token = self._get_access_token()

                    f.write('\t'.join([str(cur_time), access_token]))

                    return access_token

    def send_data(self, message):

        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()

        send_values = {

            "touser": self.TOUSER,

            "msgtype": "text",

            "agentid": self.AGENTID,

            "text": {

                "content": message

                },

            "safe": "0"

            }

        send_msges=(bytes(json.dumps(send_values), 'utf-8'))

        respone = requests.post(send_url, send_msges)

        respone = respone.json()  #当返回的数据是json串的时候直接用.json即可将respone转换成字典

        return respone["errmsg"]

#检测上海景峰网站www.jfzhiyao.com访问是否正常,每10秒通过微信发送一次消息!

def webping():

    #sum=0

    try:

        url = 'http://www.jfzhiyao.com'

        resp = urlopen(url)

        code = resp.getcode()

        #print('the result is :', code)

        if code == 200:

            if __name__ == '__main__':


                wx = WeChat()

                print('the result is :', code)

                print('网站正常!')

                wx.send_data("www.jfzhiyao.com 访问正常!\n 网站状态持续检测中……")


    except:

        if __name__ == '__main__':

            print('网站无法访问,请检查服务器!')

            wx = WeChat()

            wx.send_data("www.jfzhiyao.com 网站故障!!\n 请立即处理!!")


    Timer(300.0, webping).start() # 每10秒钟检测一次

t = Timer(300.0, webping)

t.start()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容