diff options
author | Mark Hammond <mhammond@skippinet.com.au> | 2000-07-26 07:04:38 (GMT) |
---|---|---|
committer | Mark Hammond <mhammond@skippinet.com.au> | 2000-07-26 07:04:38 (GMT) |
commit | 4f570b9239eff845c2a5298d8178cb8e01122ac2 (patch) | |
tree | c9f1ee50c8e25964d3d5d335ad05efa8365db032 /Lib | |
parent | 4d6381dfee85cdf0ef1a8172ec07619337ab960a (diff) | |
download | cpython-4f570b9239eff845c2a5298d8178cb8e01122ac2.zip cpython-4f570b9239eff845c2a5298d8178cb8e01122ac2.tar.gz cpython-4f570b9239eff845c2a5298d8178cb8e01122ac2.tar.bz2 |
Patch #100873 - Use registry to find proxies for urllib on Win32
Note that this patch looks worse than it is - an existing function (getproxies() for all platforms other than Win/Mac) has been moved, renamed and indentation changed, but the body of that function is identical. Windows now allows the environment variables to override the registry.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/urllib.py | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py index ac44249..7c9446b 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -1029,6 +1029,22 @@ def urlencode(dict): # Proxy handling +def getproxies_environment(): + """Return a dictionary of scheme -> proxy server URL mappings. + + Scan the environment for variables named <scheme>_proxy; + this seems to be the standard convention. If you need a + different way, you can pass a proxies dictionary to the + [Fancy]URLopener constructor. + + """ + proxies = {} + for name, value in os.environ.items(): + name = string.lower(name) + if value and name[-6:] == '_proxy': + proxies[name[:-6]] = value + return proxies + if os.name == 'mac': def getproxies(): """Return a dictionary of scheme -> proxy server URL mappings. @@ -1059,24 +1075,56 @@ if os.name == 'mac': # FTP: XXXX To be done. # Gopher: XXXX To be done. return proxies - -else: - def getproxies(): + +elif os.name == 'nt': + def getproxies_registry(): """Return a dictionary of scheme -> proxy server URL mappings. - - Scan the environment for variables named <scheme>_proxy; - this seems to be the standard convention. If you need a - different way, you can pass a proxies dictionary to the - [Fancy]URLopener constructor. - + + Win32 uses the registry to store proxies. + """ proxies = {} - for name, value in os.environ.items(): - name = string.lower(name) - if value and name[-6:] == '_proxy': - proxies[name[:-6]] = value + try: + import _winreg + except ImportError: + # Std module, so should be around - but you never know! + return proxies + try: + internetSettings = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, + 'Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings') + proxyEnable = _winreg.QueryValueEx(internetSettings, + 'ProxyEnable')[0] + if proxyEnable: + # Returned as Unicode but problems if not converted to ASCII + proxyServer = str(_winreg.QueryValueEx(internetSettings, + 'ProxyServer')[0]) + if ';' in proxyServer: # Per-protocol settings + for p in proxyServer.split(';'): + protocol, address = p.split('=') + proxies[protocol] = '%s://%s' % (protocol, address) + else: # Use one setting for all protocols + proxies['http'] = 'http://%s' % proxyServer + proxies['ftp'] = 'ftp://%s' % proxyServer + internetSettings.Close() + except (WindowsError, ValueError, TypeError): + # Either registry key not found etc, or the value in an + # unexpected format. + # proxies already set up to be empty so nothing to do + pass return proxies + def getproxies(): + """Return a dictionary of scheme -> proxy server URL mappings. + + Returns settings gathered from the environment, if specified, + or the registry. + + """ + return getproxies_environment() or getproxies_registry() +else: + # By default use environment variables + getproxies = getproxies_environment + # Test and time quote() and unquote() def test1(): |