utils_proxy.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. import subprocess
  3. import os
  4. import platform
  5. from base.base_load_config import load_config
  6. comfig_json = load_config()
  7. use_proxy = comfig_json.get('USE_PROXY')
  8. proxy_host = comfig_json.get('PROXY_HOST')
  9. proxy_port = comfig_json.get('PROXY_PORT')
  10. if use_proxy:
  11. system = platform.system()
  12. if system == 'Windows':
  13. env = os.environ.copy()
  14. command = ['set',
  15. 'http_proxy=http://{}:{}'.format(proxy_host, proxy_port),
  16. '&',
  17. 'set',
  18. 'https_proxy=http://{}:{}'.format(proxy_host, proxy_port)
  19. ]
  20. elif system == 'Darwin':
  21. env = os.environ.copy()
  22. command = ['export',
  23. 'https_proxy=http://{}:{}'.format(proxy_host, proxy_port),
  24. 'http_proxy=http://{}:{}'.format(proxy_host, proxy_port),
  25. 'all_proxy=socks5://{}:{}'.format(proxy_host, proxy_port)
  26. ]
  27. elif system == 'Linux':
  28. env = os.environ.copy()
  29. command = ['export',
  30. 'https_proxy=http://{}:{}'.format(proxy_host, proxy_port),
  31. 'http_proxy=http://{}:{}'.format(proxy_host, proxy_port),
  32. 'all_proxy=socks5://{}:{}'.format(proxy_host, proxy_port)
  33. ]
  34. else:
  35. print("未知操作系统")
  36. exit(0)
  37. result = subprocess.run(command, text=True, capture_output=True)
  38. print(result)