直接上代码,注意替换URL中的uid和ukey:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
import logging
import os

WHITELIST_DEBUG_MODE = 0

if "WHITELIST_DEBUG" in os.environ or WHITELIST_DEBUG_MODE: # 判断调试模式变量
logging.basicConfig(level=logging.DEBUG, format='%(message)s') # 设置日志为 Debug等级输出
logger = logging.getLogger(__name__) # 主模块
logger.debug("\nDEBUG模式开启!\n") # 消息输出
else:
logging.basicConfig(level=logging.INFO, format='%(message)s') # Info级日志
logger = logging.getLogger(__name__) # 主模块

# 自定义请求标头
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': '2024.ipchaxun.com',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"'
}

# 获取当前IP地址
def get_external_ip():
try:
response = requests.get('https://2024.ipchaxun.com/', headers=headers)
response.raise_for_status()
data = response.json()
if data.get('ret') == 'ok':
return data.get('ip')
else:
logger.info(f"获取IP地址失败,返回内容: {data}")
return None
except requests.RequestException as e:
logger.info(f"获取外部IP时出错: {e}")
return None

# 获取白名单IP
def get_whitelist_ips():
# uid和ukey换成自己的
url = 'http://op.xiequ.cn/IpWhiteList.aspx?uid=xxxxx&ukey=yyyyy&act=get'
try:
response = requests.get(url)
response.raise_for_status()
ips = response.text.split(',')
return [ip.strip() for ip in ips]
except requests.RequestException as e:
logger.info(f"获取白名单IP时出错: {e}")
return []

# 删除所有白名单记录
def delete_all_whitelist():
# uid和ukey换成自己的
url = 'http://op.xiequ.cn/IpWhiteList.aspx?uid=xxxxx&ukey=yyyyy&act=del&ip=all'
try:
response = requests.get(url)
response.raise_for_status()
logger.info("已成功删除所有白名单记录。")
except requests.RequestException as e:
logger.info(f"删除白名单记录时出错: {e}")

# 添加IP到白名单
def add_ip_to_whitelist(ip):
if ip is None:
logger.info("没有IP地址可添加。")
return
# uid和ukey换成自己的
url = f'http://op.xiequ.cn/IpWhiteList.aspx?uid=xxxxx&ukey=yyyyy&act=add&ip={ip}'
try:
response = requests.get(url)
response.raise_for_status()
if 'success' in response.text.lower():
logger.info("IP地址已成功添加到白名单。")
else:
logger.info(f"添加IP地址失败: {response.text}")
except requests.RequestException as e:
logger.info(f"添加IP到白名单时出错: {e}")

if __name__ == "__main__":
logger.info("正在获取外部IP")
ip = get_external_ip()
logger.info(f"外部IP: {ip}")

if ip:
whitelist_ips = get_whitelist_ips()
logger.info(f"当前白名单IP: {whitelist_ips}")

if ip not in whitelist_ips:
delete_all_whitelist()
add_ip_to_whitelist(ip)
else:
logger.info("当前IP已在白名单中。")

更多内容请关注我的博客