• 如果您觉得本站非常有看点,那么赶紧收藏吧
  • 如果您觉得本站非常有看点,那么赶紧收藏吧

[免流]监控广东电信跳点的小脚本

其他资源 踏雪 4个月前 (02-19) 407次浏览 0个评论

用py写的,建议用termux安装py,可以自己改一下单次跳点超过多少mb就断网,写这个的初衷就是测试模式查流量很烦,还得记以上一次用了多少流量,感觉太繁琐所以随便写个脚本挂着看看,没啥太大作用。
另外吐槽一下深圳电信,取消10元5G的流量包都得去线下营业厅,最后只能说投诉工信部才转客户经理回访取消掉。
[免流]监控广东电信跳点的小脚本

import time

import requests

# 单次跳点检测值/MB 超出自动断网
warning_flow = 1

# 查询时间间隔/s
time_interval = 120
 
# gd.189.cn登录后的cookie
cookies_str = 'apm_ct=20210809105308744; apm_uid=C990'

def get_flow():
    url = 'https://gd.189.cn/J/J10014.j'
    
    headers = {
        # Host: gd.189.cn
        'Referer': 'https://gd.189.cn/service/home/query/flow_check.html?in_cmpid=gdsb-zzfw-llxq2018',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
        'Cookie': cookies_str
    }
    res = requests.get(url, headers=headers)
    # print(res.json())
    ls = res.json()['r']['r02']
    # for date in ls:
    #     print(date['r0201'], date['r0202'])
    return ls


def get_time():
    ticks = time.time()
    return time.strftime("%m.%d-%H:%M:%S")


if __name__ == '__main__':

    # 初始化流量数据
    flow_ls_init = get_flow()
    sum_flow_init = 0
    # 字段名
    print('时间', end='\t')
    for dic in flow_ls_init:
        print(format(dic['r0201']), end='\t')
    print('跳点/MB')
    # 流量初始化
    print(get_time(), end='\t')
    for dic in flow_ls_init:
        sum_flow_init += dic['r0202']
        print(format(dic['r0202']), end='\t')
    print('0')
    # 自定义查询时间间隔,默认120s
    time.sleep(time_interval)
    while True:

        try:
            flow_ls = get_flow()
            if len(flow_ls) == 0:
                continue
                
            sum_flow = 0
            for dic in flow_ls:
                sum_flow += dic['r0202']

            # 总流量没变化跳出循环
            if sum_flow_init == sum_flow:
                time.sleep(120)
                continue

            # 流量有变化,更新初始化数组
            flow_ls_init = flow_ls
            # 打印新的流量信息
            print(get_time(), end='\t')
            for dic in flow_ls_init:
                print(format(dic['r0202']), end='\t')

            # 打印差值
            used_flow = sum_flow - sum_flow_init
            print(used_flow)
            
            # 判断差值并执行业务逻辑
            if used_flow > 1:
                print('本次跳点超过:{}MB,将关闭数据'.format(warning_flow))
                # os.system("su -c /data/data/com.termux/files/home/自定义脚本.sh")
            sum_flow_init = sum_flow

        except Exception as e:
            print(get_time(), '此次查询错误')
        time.sleep(120)

踏雪爱分享 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:[免流]监控广东电信跳点的小脚本
喜欢 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)