switch2global.py 1.1 KB

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