携趣 ip 自动更新 ip 白名单脚本

一、介绍以及免费试用教程

携趣 http://util.hnking.cn/api/short/Zi1EII(每日免费 1000ip)

其他类似的动态代理 API 也同样支持
随便选择一个代理网站注册,注册完后领取每日白嫖套餐。
本教程以携趣为例。

1. 注册实名认证

实名认证是支付宝支付宝。
在这里插入图片描述

2. 先添加白名单

把你的服务器 ip 填上去就行了
在这里插入图片描述

3. 提取 API

如图所示配置点击生成连接
在这里插入图片描述

二、自动更新白名单脚本

1. 脚本

import requests import os CACHE_FILE = 'whitelist_cache.txt' API_URL = "http://op.xiequ.cn/IpWhiteList.aspx" PARAMS = { 'uid': '你的uid', 'ukey': '你的key', } def get_current_ip(): """获取当前 IP 地址""" try: response = requests.get('https://httpbin.org/ip') response.raise_for_status() res = response.json().get('origin') return res except requests.exceptions.RequestException as e: print(f"Failed to fetch the current IP: {e}") return get_current_ip2() # https://ip.3322.net def get_current_ip2(): api_url = f"https://ip.3322.net" try: response = requests.get(api_url) response.raise_for_status() return response.text except requests.exceptions.RequestException as e: print(f"Failed to fetch information for IP: {e}") def fetch_ip_whitelist(): """从 API 获取当前的 IP 白名单""" params = PARAMS.copy() params['act'] = 'get' try: print("Fetching the IP whitelist from API...") response = requests.get(API_URL, params=params) response.raise_for_status() print("IP whitelist fetched successfully.") return response.text.strip() except requests.exceptions.RequestException as e: print(f"Failed to fetch the IP whitelist: {e}") return None def add_ip_to_whitelist(ip): """向白名单中添加一个 IP 地址""" params = PARAMS.copy() params.update({'act': 'add', 'ip': ip}) try: print(f"Adding IP {ip} to whitelist...") response = requests.get(API_URL, params=params) res = response.raise_for_status() print(res) print(response.text) print(f"IP {ip} added to whitelist successfully.") except requests.exceptions.RequestException as e: print(f"Failed to add IP {ip} to whitelist: {e}") def delete_ip_from_whitelist(ip): """从白名单中删除一个 IP 地址""" params = PARAMS.copy() params.update({'act': 'del', 'ip': ip}) try: print(f"Deleting IP {ip} from whitelist...") response = requests.get(API_URL, params=params) response.raise_for_status() print(f"IP {ip} deleted from whitelist successfully.") except requests.exceptions.RequestException as e: print(f"Failed to delete IP {ip} from whitelist: {e}") def get_ip_whitelist_from_cache(): """从缓存文件中获取 IP 白名单,如果文件不存在或为空则从 API 获取并更新缓存""" if not os.path.exists(CACHE_FILE) or os.path.getsize(CACHE_FILE) == 0: return "" else: # 读取缓存文件中的内容 with open(CACHE_FILE, 'r') as f: return f.read().strip() def update_cache_with_new_ip(ip): """将新的 IP 添加到缓存文件中""" current_ips = get_ip_whitelist_from_cache() new_ips = current_ips + ("" if not current_ips else "\n") + ip with open(CACHE_FILE, 'w') as f: f.write(new_ips) def update(ip, cache_ip): print(f"上次缓存中的IP: {cache_ip}") ## 获取白名单ip white_ip = fetch_ip_whitelist() # 如果服务器包含当前IP 在白名单不做添加 if ip not in white_ip: print("IP地址不一致,更新白名单") update_cache_with_new_ip(ip) add_ip_to_whitelist(ip) delete_ip_from_whitelist(cache_ip) else: print("IP地址一致,无需更新白名单") def update1(ip): ## 获取白名单ip white_ip = fetch_ip_whitelist() # 如果服务器包含当前IP 在白名单不做添加 if ip not in white_ip: print("IP地址不一致,更新白名单") update_cache_with_new_ip(ip) add_ip_to_whitelist(ip) else: print("IP地址一致,无需更新白名单") def update_ip1(): ip = get_current_ip() print(f"服务器当前IP: {ip}") # 示例调用:从缓存或 API 获取 IP 白名单 cache_ip = get_ip_whitelist_from_cache() ## 如果缓存文件不存在,则从 API 获取最新数据 if not cache_ip: print("缓存文件不存在,从API获取最新数据") cache_ip = ip update_cache_with_new_ip(cache_ip) update1(ip) else: update(ip, cache_ip) if __name__ == "__main__": update_ip1()

2. uid,ukey 获取方式

  1. 首页点击 IP 白名单授权
    在这里插入图片描述
  2. 在白名单授权页面下拉找到白名单管理接口
    找到对应的 uid,ukey 填写进本脚本
    在这里插入图片描述

3. 使用方式(以青龙面板为例)

  1. 在脚本管理中新建一个名字为 ip.py 的文件,并且把脚本粘贴进去
    在这里插入图片描述
  2. 在定时任务中新建任
    任务名:更新白名单;
    命令为:task ip.py
    定时规则:0 * * * * (代表一小时更新一次,可自己按需求更改)
    在这里插入图片描述

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...