proxy_uploader.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import requests
  2. from datetime import datetime, timezone
  3. # --- 配置部分 ---
  4. API_URL = 'https://api.text.skin/api/proxy/create'
  5. BEARER_TOKEN = 'tok_e946329a60ff45ba807f3f41b0e8b7fc' # 替换为你的真实 Token
  6. FILE_PATH = '../config/decodo.txt' # 替换为你实际的文本文件名 (例如 proxies.txt)
  7. headers = {
  8. 'accept': 'application/json',
  9. 'Authorization': f'Bearer {BEARER_TOKEN}',
  10. 'Content-Type': 'application/json'
  11. }
  12. def main():
  13. proxies = []
  14. # 1. 逐行读取文本文件
  15. try:
  16. with open(FILE_PATH, 'r', encoding='utf-8') as f:
  17. for line in f:
  18. line = line.strip()
  19. if not line:
  20. continue # 跳过空行
  21. # 按冒号分割: IP/域名:端口:账号:密码
  22. # 使用 split(':', 3) 防止密码中碰巧包含冒号导致分割错误
  23. parts = line.split(':', 3)
  24. if len(parts) == 4:
  25. proxies.append({
  26. "ip": parts[0],
  27. "port": int(parts[1]), # 端口需转为数字
  28. "username": parts[2],
  29. "password": parts[3]
  30. })
  31. else:
  32. print(f"警告: 格式不符合预期(应为 IP:端口:用户名:密码),已跳过该行 -> {line}")
  33. except FileNotFoundError:
  34. print(f"错误: 找不到文件 {FILE_PATH}")
  35. return
  36. if not proxies:
  37. print("没有读取到任何有效的代理,请检查文件格式。")
  38. return
  39. print(f"共读取到 {len(proxies)} 个代理,准备开始上传...\n")
  40. # 2. 遍历列表并发送请求
  41. success_count = 0
  42. fail_count = 0
  43. for index, proxy in enumerate(proxies, start=1):
  44. # 组装 API Payload
  45. payload = {
  46. "pool_name": "decodo", # 自定义你的代理池名称
  47. "proto": "http", # 文本中没有协议字段,默认为 http。如果是 socks5 请自行修改
  48. "ip": proxy["ip"],
  49. "port": proxy["port"],
  50. "username": proxy["username"],
  51. "password": proxy["password"],
  52. # 设置下次使用时间(当前UTC时间)
  53. "next_use_time": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
  54. "status": "active" # 默认状态
  55. }
  56. # 3. 发送 POST 请求
  57. try:
  58. response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
  59. # 判断响应状态码
  60. if response.status_code in (200, 201):
  61. print(f"[{index}/{len(proxies)}] 成功: 上传 {payload['ip']}:{payload['port']}")
  62. success_count += 1
  63. else:
  64. print(f"[{index}/{len(proxies)}] 失败: {payload['ip']}:{payload['port']} | HTTP状态码: {response.status_code} | 详情: {response.text}")
  65. fail_count += 1
  66. except requests.exceptions.RequestException as e:
  67. print(f"[{index}/{len(proxies)}] 请求异常: {payload['ip']}:{payload['port']} | 错误: {e}")
  68. fail_count += 1
  69. # 打印最终统计
  70. print("\n--- 上传完成 ---")
  71. print(f"成功: {success_count} 条")
  72. print(f"失败: {fail_count} 条")
  73. if __name__ == "__main__":
  74. main()