switch2global.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. # -*- coding: utf-8 -*-
  2. import httpx
  3. import asyncio
  4. ip = "192.168.31.194"
  5. port_list = ['58001', '58002', '58003', '58004', '58005', '58006', '58007', '58008', '58009', '58010']
  6. # port_list = ['32001', '32002', '32003', '32004', '32005', '32006', '32007', '32008', '32009', '32010', '32011', '32012']
  7. async def patch_config(base_port):
  8. url_and_port = f"http://{ip}:{base_port}"
  9. key = "/api/configs"
  10. full_url = url_and_port + key
  11. data = {"mode": "Global"}
  12. headers = {"Content-Type": "application/json"}
  13. async with httpx.AsyncClient() as client:
  14. try:
  15. response = await client.patch(full_url, json=data, headers=headers)
  16. response.raise_for_status()
  17. print(f"{url_and_port}: 切换全局代理 OK")
  18. except httpx.HTTPError as exc:
  19. print(f"请求失败: {exc}")
  20. exit(1)
  21. async def main():
  22. tasks = [patch_config(base_port) for base_port in port_list]
  23. await asyncio.gather(*tasks)
  24. if __name__ == "__main__":
  25. asyncio.run(main())