switch2global.py 1.1 KB

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