| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import json
- import requests
- from datetime import datetime, timezone
- # --- 配置部分 ---
- API_URL = 'http://localhost:8888/api/proxy/create'
- BEARER_TOKEN = 'tok_5d84b9ecddfa4386a995bf8996901828' # 替换为你的真实 Token
- FILE_PATH = 'config/proxies.json'
- headers = {
- 'accept': 'application/json',
- 'Authorization': f'Bearer {BEARER_TOKEN}',
- 'Content-Type': 'application/json'
- }
- def main():
- # 1. 读取 JSON 文件
- try:
- with open(FILE_PATH, 'r', encoding='utf-8') as f:
- data = json.load(f)
- except FileNotFoundError:
- print(f"错误: 找不到文件 {FILE_PATH}")
- return
- except json.JSONDecodeError:
- print(f"错误: {FILE_PATH} 不是有效的 JSON 格式")
- return
- proxies = data.get('isp_all', [])
- if not proxies:
- print("没有在 JSON 中找到 'isp_all' 列表。")
- return
- print(f"共读取到 {len(proxies)} 个代理,准备开始上传...\n")
- # 2. 遍历列表并发送请求
- success_count = 0
- fail_count = 0
- for index, proxy in enumerate(proxies, start=1):
- # 字段映射转换
- payload = {
- "pool_name": "isp_all", # 你可以根据需要自定义这个代理池名称
- "proto": proxy.get("scheme", "http"), # scheme 映射到 proto
- "ip": proxy.get("ip"),
- "port": proxy.get("port"),
- "username": proxy.get("username", ""),
- "password": proxy.get("password", ""),
- # 设置一个默认的下次使用时间(这里使用当前UTC时间)
- "next_use_time": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
- "status": "active" # 设定初始状态,例如 "active" 或 "ready"
- }
- # 3. 发送 POST 请求
- try:
- response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
-
- # 判断响应状态码 (200 或 201 通常表示创建成功)
- if response.status_code in (200, 201):
- print(f"[{index}/{len(proxies)}] 成功: 上传 {payload['ip']}:{payload['port']}")
- success_count += 1
- else:
- print(f"[{index}/{len(proxies)}] 失败: {payload['ip']}:{payload['port']} | HTTP状态码: {response.status_code} | 详情: {response.text}")
- fail_count += 1
-
- except requests.exceptions.RequestException as e:
- print(f"[{index}/{len(proxies)}] 请求异常: {payload['ip']}:{payload['port']} | 错误: {e}")
- fail_count += 1
- # 打印最终统计
- print("\n--- 上传完成 ---")
- print(f"成功: {success_count} 条")
- print(f"失败: {fail_count} 条")
- if __name__ == "__main__":
- main()
|