Python, get setting of proxy from OS

2019-09-09 23:46发布

问题:

I need to get some data from the internet but I'm behind corporate proxy and I do not know a password. So I tried many examples with pyRequests module but always I get only message 'Authorization failed ...'.

So I am just thinking if this idea is possible: Main setup for proxy is stored in the IE (connection tab in settings) and I am also using Chrome and if I understand well Chrome is using this setup for access to internet (because when I open proxy setting in Chrome, the IE Proxy Setting Window popup).

So my question is if I can also use (somehow) this setting for my python script and how (without knowing password).

回答1:

By using the Python urllib module you can programmatically obtain the proxies on your system in a platform independent way using the urllib.request.getproxies function:

import urllib
urllib.request.getproxies()

This function will first check for any proxies set in the environment variables (e.g. http_proxy etc) or if none are set then it will check for system configured proxies using platform specific calls (e.g. On MacOS it will check using the system scutil/configd interfaces, and on Windows it will check the Registry).



回答2:

There are known (UNIX) operating system environment variables whi Python support for proxying. Also, for Windows, it should try to read settings from OS registry in the case it is possible: https://mail.python.org/pipermail/tutor/2008-December/065573.html

Proxy settings are used e.g. by

  • pip

  • urllib

  • requests

... as far as I know.

If Python fails to get proxy settings from Windows it would be worth of investigating by running test scripts which use urllib proxy functions (see link above).

Examples how to set HTTP(S) proxy for Python:

How to tell Python to automatically use the proxy setting in Windows XP like R's internet2 option?