proxy_uploader.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import json
  2. import requests
  3. from datetime import datetime, timezone
  4. # --- 配置部分 ---
  5. API_URL = 'http://localhost:8888/api/proxy/create'
  6. BEARER_TOKEN = 'tok_5d84b9ecddfa4386a995bf8996901828' # 替换为你的真实 Token
  7. FILE_PATH = 'config/proxies.json'
  8. headers = {
  9. 'accept': 'application/json',
  10. 'Authorization': f'Bearer {BEARER_TOKEN}',
  11. 'Content-Type': 'application/json'
  12. }
  13. def main():
  14. # 1. 读取 JSON 文件
  15. try:
  16. with open(FILE_PATH, 'r', encoding='utf-8') as f:
  17. data = json.load(f)
  18. except FileNotFoundError:
  19. print(f"错误: 找不到文件 {FILE_PATH}")
  20. return
  21. except json.JSONDecodeError:
  22. print(f"错误: {FILE_PATH} 不是有效的 JSON 格式")
  23. return
  24. proxies = data.get('isp_all', [])
  25. if not proxies:
  26. print("没有在 JSON 中找到 'isp_all' 列表。")
  27. return
  28. print(f"共读取到 {len(proxies)} 个代理,准备开始上传...\n")
  29. # 2. 遍历列表并发送请求
  30. success_count = 0
  31. fail_count = 0
  32. for index, proxy in enumerate(proxies, start=1):
  33. # 字段映射转换
  34. payload = {
  35. "pool_name": "isp_all", # 你可以根据需要自定义这个代理池名称
  36. "proto": proxy.get("scheme", "http"), # scheme 映射到 proto
  37. "ip": proxy.get("ip"),
  38. "port": proxy.get("port"),
  39. "username": proxy.get("username", ""),
  40. "password": proxy.get("password", ""),
  41. # 设置一个默认的下次使用时间(这里使用当前UTC时间)
  42. "next_use_time": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
  43. "status": "active" # 设定初始状态,例如 "active" 或 "ready"
  44. }
  45. # 3. 发送 POST 请求
  46. try:
  47. response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
  48. # 判断响应状态码 (200 或 201 通常表示创建成功)
  49. if response.status_code in (200, 201):
  50. print(f"[{index}/{len(proxies)}] 成功: 上传 {payload['ip']}:{payload['port']}")
  51. success_count += 1
  52. else:
  53. print(f"[{index}/{len(proxies)}] 失败: {payload['ip']}:{payload['port']} | HTTP状态码: {response.status_code} | 详情: {response.text}")
  54. fail_count += 1
  55. except requests.exceptions.RequestException as e:
  56. print(f"[{index}/{len(proxies)}] 请求异常: {payload['ip']}:{payload['port']} | 错误: {e}")
  57. fail_count += 1
  58. # 打印最终统计
  59. print("\n--- 上传完成 ---")
  60. print(f"成功: {success_count} 条")
  61. print(f"失败: {fail_count} 条")
  62. if __name__ == "__main__":
  63. main()