python监控验证码

环境验证:一键安装包

pip3 install aliyun-python-sdk-core

pip3 install aliyunsdkcore

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/5/23 5:32 PM
# @Author  : zk
# @File    : sms.py
# @Software: PyCharm
 
import requests
import urllib
import logging
import os
import time
import datetime
import socket
import json
import hmac
import hashlib
import base64
import urllib.parse
# from time import time
import requests
import socket
 
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkdysmsapi.request.v20170525.SendSmsRequest import SendSmsRequest
credentials = AccessKeyCredential('', '')
 
 
 
logging.basicConfig(level=logging.INFO,
                    filename='log/my_code.log',
                    filemode='a',
                    format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    )
 
def get_digest():
    timestamp = str(round(time.time() * 1000))
    #测试
    secret = ''
    #生产
    #secret = ''
    secret_enc = secret.encode('utf-8')  # utf-8编码
    string_to_sign = '{}\n{}'.format(timestamp, secret)  # 字符串格式化拼接
    string_to_sign_enc = string_to_sign.encode('utf-8')  # utf-8编码
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()  # HmacSHA256算法计算签名
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))  # Base64编码后进行urlEncode
    #  返回时间戳和计算好的编码拼接字符串,后面直接拼接到Webhook即可
    return f"×tamp={timestamp}&sign={sign}"
def get_system_info():
    # 系统时间
    dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 
    # 系统公网IP
    ip = requests.get('http://ifconfig.me').text.strip()
 
    # 内网IP
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 80))
    local_ip = s.getsockname()[0]
    s.close()
 
    #系统load
    system_load=os.popen("uptime|awk '{print $NF,$NF-1,$NF-2}'")
    load=system_load.read().strip()
 
    host_name=socket.gethostname()
    return dt, ip, local_ip,load,host_name
def sed_dingding(title,Operational_behavior,code):
    MSG = "**告警主题**:" +  "\n" + title  + "\n\n" \
          "- 数据查询信息入下:" + "\n\n"   "\n\n" \
         ">![]()  环境地址:**[一键安装包](https://www.zkong.com/)**\n\n" \
         " >当前主机名:"  + "\n" + get_system_info()[4] + "\n\n" \
         " >当前服务器IP:" + "\n" + get_system_info()[1] + "\n" + get_system_info()[2] +"\n\n" \
         " >当前系统load:" + "\n" + str(get_system_info()[3]) + "\n\n" \
         " >系统操作行为:" + "\n" + Operational_behavior +  "\n" + code + "\n\n" \
 
 
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": MSG,
 
        },
        "at": {
            "atMobiles": [
                "@15657199983"  # 要@对象的手机号,在文本message里也需要加上@用户的手机号
            ],
            "isAtAll": False  # 是否要@所有人
        }
    }
    # 机器人链接地址,发post请求 向钉钉机器人传递指令
    #测试
    webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token='
    #生产
    #webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token='
    # 利用requests发送post请求
    req = requests.post(webhook_url + get_digest(), json=data)
 
 
class SmsAlarm:
 
    def __init__(self, phone, sign, template, ):
        self.phonenumbers = phone
        self.signname = sign
        self.templatecode = template
 
 
 
    def get_sms_code(self):
        print(self.phonenumbers)
        print(self.signname)
        print(self.templatecode)
        client = AcsClient(region_id='cn-hangzhou', credential=credentials)
 
        request = SendSmsRequest()
        request.set_accept_format('json')
 
        request.set_PhoneNumbers(self.phonenumbers)
        request.set_SignName(self.signname)
        request.set_TemplateCode(self.templatecode)
 
        response = client.do_action_with_exception(request)
        # python2:  print(response)
        print(str(response, encoding='utf-8'))
 
 
# obj = SmsAlarm(, '', '')
 
# obj.get_sms_code()
 
class GetCode:
    def __init__(self, url):
        self.url = url
    def get_url_code(self):
        res = requests.get(self.url)
        code = res.status_code
 
        title="esl-business服务异常"
        system_msg="正在重启,当前状态码为:"
        #read old code
        with open('log/status') as file:
            files=str(file.read())
        # old_code != new_code
        if str(files) != str(12):
            with open('log/status','w') as rw:
                file_rw=rw.write(str(code))
 
                logging.error("Program exception, restarting, sending restart alarm, current status code:{}".format(code))
                logging.info("Stop for three minutes")
                #res_zk = os.system("docker restart zk-refactor-esl-business >>/dev/null")
                #钉钉通知
                sed_dingding(title,system_msg,str(code))
                #短信通知
                Song = SmsAlarm(, '', '')
                Song.get_sms_code()
 
 
        else:
            logging.info("The program is running normally. Current status code:{}".format(code))
 
if "__name__ == __main__":
    obj = GetCode('http://:81/user/?loginType=1')
    obj.get_url_code()

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
采用docker部署爬虫模块、量化指标算法模块、订阅分发模块等,完成爬取股票数据存入通用数据库接口,并缓存至redis;算法模块处理数据,将报警信息存入redis;订阅分发模块监控到有报警信息后主动向微信订阅用户推送;同时部署有微信服务器,完成与微信用户的交互。 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
目录 第1章 Python 处理 cassandra 升级后的回滚脚本 第 2 章 多套方案来提高 python web 框架的并发处理能力 第 3 章 python 写报警程序中的声音实现 winsound 第 4 章 一个脚本讲述 python 语言的基础规范,适合初学者 第 5 章 python 计算文件的行数和读取某一行内容的实现方法 第 6 章 python 中用 string.maketrans 和 translate 巧妙替换字符串 第 7 章 python linecache 模块读取文件用法详解 第 8 章 python 调用 zabbix 的 api 接口添加主机、查询组、主机、模板 第 9 章 python+Django 实现 Nagios 自动化添加监控项目 第 10 章 通过 python 和 websocket 构建实时通信系统[扩展 saltstack 监控] 第 11 章 关于 B+tree (附 python 模拟代码) 第 12 章 Python 编写的 socket 服务器和客户端 第 13 章 python 之 MySQLdb 库的使用 第 14 章 python 监控文件或目录变化 第 15 章 Mongodb 千万级数据在 python 下的综合压力测试及应用探讨 第 16 章 通过 memcached 实现领号排队功能及 python 队列实例. 第 17 章 python 之利用 PIL 库实现页面的图片验证码及缩略图 第 18 章 如何将 Mac OS X10.9 下的 Python2.7 升级到最新的 Python3.3 第 19 章 使用 python 构建基于 hadoop 的 mapreduce 日志分析平台 第 20 章 报警监控平台扩展功能 url 回调的设计及应用 [python 语言] 第 21 章 服务端 socket 开发之多线程和 gevent 框架并发测试[python 语言] 第 22 章 利用 pypy 提高 python 脚本的执行速度及测试性能 第 23 章 python 实现 select 和 epoll 模型 socket 网络编程 第 24 章 对 Python-memcache 分布式散列和调用的实现 第 25 章 Parallel Python 实现程序的并行多 cpu 多核利用【pp 模块】 第 26 章 关于 python 调用 zabbix api 接口的自动化实例 [结合 saltstack] 第 27 章 Python 批量更新 nginx 配置文件 第 28 章 Python 通过 amqp 消息队列协议中的 Qpid 实现数据通信 第 29 章 python simplejson 模块浅谈 第 30 章 python Howto 之 logging 模块 第 31 章 Python FAQ3-python 中 的原始(raw)字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值