clash_set_global.py 1.1 KB

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