clash_set_global.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. # 所有节点切换为全局
  3. import asyncio
  4. import httpx
  5. async def set_global(url_and_port):
  6. url = f"http://{url_and_port}"
  7. key = "/api/configs"
  8. full_url = url + key
  9. data = {"mode": "Global"}
  10. headers = {"Content-Type": "application/json"}
  11. async with httpx.AsyncClient(timeout=10.0) as client:
  12. try:
  13. response = await client.patch(full_url, json=data, headers=headers)
  14. response.raise_for_status() # 自动抛出4xx/5xx异常
  15. print(f"成功设置 {url_and_port}")
  16. return True
  17. except httpx.HTTPError as exc:
  18. print(f"请求失败 {url_and_port}: {exc}")
  19. return False
  20. async def main():
  21. ip = '192.168.31.201'
  22. port_list = [f'{58000 + i}' for i in range(1, 11)] # 生成端口列表
  23. # 创建任务列表
  24. tasks = [set_global(f"{ip}:{port}") for port in port_list]
  25. # 并发执行所有任务
  26. results = await asyncio.gather(*tasks)
  27. # 统计结果
  28. success_count = sum(results)
  29. print(f"\n完成设置: {success_count}/{len(port_list)} 个代理成功")
  30. if __name__ == "__main__":
  31. asyncio.run(main())