From 7571388d057b60cca762433523e6d28f9f6ac337 Mon Sep 17 00:00:00 2001 From: fofo Date: Wed, 4 Jun 2025 17:39:21 +0800 Subject: [PATCH] fix python3.10 --- README.MD | 110 ++++++++++++++++++++++++++++++++++++++++++++++------ config.cfg | 2 +- renew_ip.py | 8 +++- 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/README.MD b/README.MD index 759336a..6a05759 100644 --- a/README.MD +++ b/README.MD @@ -1,16 +1,104 @@ -# 配置config.cfg +# IP自动续费工具 -* url : 代理获取url -* index: ip白名单编号,不同的机器配置不同编号,编号从0开始(相同公网IP的内网机器使用相同的编号) -* wait: 更新IP间隔,单位秒 +这是一个用于自动检测公网IP变化并更新代理白名单的工具。当检测到公网IP发生变化时,会自动调用API更新指定索引位置的IP白名单。 -# 安装运行 -* pip install -r requirements.txt -* nohup python renew_ip.py & +## 功能特性 +- 自动检测公网IP变化 +- IP变化时自动更新代理白名单 +- 支持Python 2.7和Python 3.x +- 可配置检测间隔和白名单索引 +- 详细日志记录 -# API参考 -添加/修改白名单IP +## 配置文件 config.cfg -* 在链接后面加上 &limitip=append&newip=[ip] ,[ip] 需要添加的ip地址,如果数量超过限制则添加失败 -* 在链接后面加上 &limitip=append&newip=[ip]&index=-1 ,[ip] 需要添加的新IP地址 ,[index] 需要修改的旧IP索引位置,-1为列表中最后一个,0为列表中第一个 \ No newline at end of file +创建`config.cfg`文件并配置以下参数: + +```ini +[main] +# 代理API的URL地址 +url=http://ip.16yun.cn:817/myip/pl/b1c75840-40a0-115b-1173-3a66ce51110f/?s=amypvklrkb&u=test + +# IP白名单编号,从0开始 +# 不同机器配置不同编号,相同公网IP的内网机器使用相同编号 +index=0 + +# IP检测更新间隔,单位:秒 +wait=60 +``` + +### 配置参数说明 + +- **url**: 代理服务商提供的API接口地址,用于更新IP白名单 +- **index**: IP白名单编号,从0开始计数 + - 不同的机器应配置不同的编号 + - 位于相同公网IP下的内网机器可以使用相同编号 +- **wait**: IP检测间隔时间,单位为秒(建议不少于30秒) + +## 安装运行 + +1. 安装依赖包: +```bash +pip install -r requirements.txt +``` + +2. 创建requirements.txt文件(如果不存在): +``` +requests +``` + +3. 配置config.cfg文件 + +4. 运行程序: +```bash +# 前台运行 +python renew_ip.py + +# 后台运行 +nohup python renew_ip.py & +``` + +## 工作原理 + +1. 程序启动后会定期(根据wait配置)检测当前公网IP +2. 通过访问 `http://current-ip.16yun.cn:802` 获取当前公网IP +3. 如果检测到IP发生变化,会调用配置的API接口更新白名单 +4. 更新时会在API URL后添加参数:`index=[配置的索引]&newip=[新IP]` +5. 所有操作都会记录到`log.txt`日志文件中 + +## 日志文件 + +程序运行时会在当前目录生成`log.txt`日志文件,记录: +- IP检测结果 +- IP变化通知 +- API调用结果 +- 错误信息 + +## 兼容性 + +- 支持Python 2.7 +- 支持Python 3.x +- 跨平台兼容(Windows/Linux/macOS) + +## API参考 + +### 白名单IP操作 + +基于代码逻辑,API调用方式: + +**自动更新指定索引的IP:** +- 程序会自动在配置的URL后添加参数:`&index=[索引号]&newip=[新IP地址]` +- 例如:`原URL&index=0&newip=192.168.1.100` + +**手动API调用参考:** +- 添加IP到白名单:在链接后面加上 `&limitip=append&newip=[ip]` +- 修改指定位置IP:在链接后面加上 `&limitip=append&newip=[ip]&index=[索引]` + - `[ip]`: 需要添加/修改的IP地址 + - `[index]`: IP在白名单中的索引位置,-1为最后一个,0为第一个 + +## 注意事项 + +1. 确保网络连接正常,能够访问IP检测和API接口 +2. API接口需要有效,否则IP更新会失败 +3. 建议将wait时间设置为60秒或更长,避免频繁请求 +4. 定期检查日志文件,确保程序正常运行 \ No newline at end of file diff --git a/config.cfg b/config.cfg index 0e23253..7c553a4 100644 --- a/config.cfg +++ b/config.cfg @@ -1,6 +1,6 @@ [main] # 代理url -url=http://ip.16yun.cn:817/myip/pl/b1c75840-40a0-115b-1173-3a66ce51110f/?s=amypvklrkb&u=test +url=http://ip.16yun.cn:817/myip/pl/1111111-40a0-115b-1173-3a66ce51110f/?s=2222222&u=test # ip编号,0为第一个 index=0 diff --git a/renew_ip.py b/renew_ip.py index 0242ca2..223f706 100644 --- a/renew_ip.py +++ b/renew_ip.py @@ -122,9 +122,13 @@ def main(url, index, wait): if __name__ == "__main__": - import ConfigParser + try: + import ConfigParser as configparser + except ImportError: + # Python 3 fallback + import configparser - config = ConfigParser.ConfigParser() + config = configparser.ConfigParser() config.readfp(open('config.cfg')) url = config.get("main", "url") index = config.getint("main", "index")