Return a dictionary of scheme -> proxy server URL mappings. Win32 uses the registry to store proxies.
()
| 2069 | |
| 2070 | elif os.name == 'nt': |
| 2071 | def getproxies_registry(): |
| 2072 | """Return a dictionary of scheme -> proxy server URL mappings. |
| 2073 | |
| 2074 | Win32 uses the registry to store proxies. |
| 2075 | |
| 2076 | """ |
| 2077 | proxies = {} |
| 2078 | try: |
| 2079 | import winreg |
| 2080 | except ImportError: |
| 2081 | # Std module, so should be around - but you never know! |
| 2082 | return proxies |
| 2083 | try: |
| 2084 | internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER, |
| 2085 | r'Software\Microsoft\Windows\CurrentVersion\Internet Settings') |
| 2086 | proxyEnable = winreg.QueryValueEx(internetSettings, |
| 2087 | 'ProxyEnable')[0] |
| 2088 | if proxyEnable: |
| 2089 | # Returned as Unicode but problems if not converted to ASCII |
| 2090 | proxyServer = str(winreg.QueryValueEx(internetSettings, |
| 2091 | 'ProxyServer')[0]) |
| 2092 | if '=' not in proxyServer and ';' not in proxyServer: |
| 2093 | # Use one setting for all protocols. |
| 2094 | proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer) |
| 2095 | for p in proxyServer.split(';'): |
| 2096 | protocol, address = p.split('=', 1) |
| 2097 | # See if address has a type:// prefix |
| 2098 | if not re.match('(?:[^/:]+)://', address): |
| 2099 | # Add type:// prefix to address without specifying type |
| 2100 | if protocol in ('http', 'https', 'ftp'): |
| 2101 | # The default proxy type of Windows is HTTP |
| 2102 | address = 'http://' + address |
| 2103 | elif protocol == 'socks': |
| 2104 | address = 'socks://' + address |
| 2105 | proxies[protocol] = address |
| 2106 | # Use SOCKS proxy for HTTP(S) protocols |
| 2107 | if proxies.get('socks'): |
| 2108 | # The default SOCKS proxy type of Windows is SOCKS4 |
| 2109 | address = re.sub(r'^socks://', 'socks4://', proxies['socks']) |
| 2110 | proxies['http'] = proxies.get('http') or address |
| 2111 | proxies['https'] = proxies.get('https') or address |
| 2112 | internetSettings.Close() |
| 2113 | except (OSError, ValueError, TypeError): |
| 2114 | # Either registry key not found etc, or the value in an |
| 2115 | # unexpected format. |
| 2116 | # proxies already set up to be empty so nothing to do |
| 2117 | pass |
| 2118 | return proxies |
| 2119 | |
| 2120 | def getproxies(): |
| 2121 | """Return a dictionary of scheme -> proxy server URL mappings. |