diff options
author | Ronald Oussoren <ronaldoussoren@mac.com> | 2010-04-18 20:49:34 (GMT) |
---|---|---|
committer | Ronald Oussoren <ronaldoussoren@mac.com> | 2010-04-18 20:49:34 (GMT) |
commit | 218cc58d08095efb05d62e7a7d58853a33dd2d12 (patch) | |
tree | 1a24b77cef873fa16bf108a7764b69c6a8c7211a /Lib/urllib | |
parent | b136a9c9d718e6cc717aef45daec9e8b28423b06 (diff) | |
download | cpython-218cc58d08095efb05d62e7a7d58853a33dd2d12.zip cpython-218cc58d08095efb05d62e7a7d58853a33dd2d12.tar.gz cpython-218cc58d08095efb05d62e7a7d58853a33dd2d12.tar.bz2 |
Merged revisions 80198 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r80198 | ronald.oussoren | 2010-04-18 22:46:11 +0200 (Sun, 18 Apr 2010) | 4 lines
For for issue #7154: Port the code that uses
the SystemConfiguration framework to detect the
proxy settings on OSX from the trunk to python 3.2
........
Diffstat (limited to 'Lib/urllib')
-rw-r--r-- | Lib/urllib/request.py | 92 |
1 files changed, 65 insertions, 27 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index d1d12e6..0f9e2f8 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2142,44 +2142,82 @@ def proxy_bypass_environment(host): if sys.platform == 'darwin': - def getproxies_internetconfig(): - """Return a dictionary of scheme -> proxy server URL mappings. + from _scproxy import _get_proxy_settings, _get_proxies - By convention the mac uses Internet Config to store - proxies. An HTTP proxy, for instance, is stored under - the HttpProxy key. + def proxy_bypass_macosx_sysconf(host): + """ + Return True iff this host shouldn't be accessed using a proxy + This function uses the MacOSX framework SystemConfiguration + to fetch the proxy information. """ - try: - import ic - except ImportError: - return {} + import re + import socket + from fnmatch import fnmatch + + hostonly, port = splitport(host) + + def ip2num(ipAddr): + parts = ipAddr.split('.') + parts = map(int, parts) + if len(parts) != 4: + parts = (parts + [0, 0, 0, 0])[:4] + return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3] + + proxy_settings = _get_proxy_settings() + + # Check for simple host names: + if '.' not in host: + if proxy_settings['exclude_simple']: + return True + + hostIP = None + + for value in proxy_settings.get('exceptions', ()): + # Items in the list are strings like these: *.local, 169.254/16 + if not value: continue + + m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value) + if m is not None: + if hostIP is None: + try: + hostIP = socket.gethostbyname(hostonly) + hostIP = ip2num(hostIP) + except socket.error: + continue + + base = ip2num(m.group(1)) + mask = int(m.group(2)[1:]) + mask = 32 - mask + + if (hostIP >> mask) == (base >> mask): + return True + + elif fnmatch(host, value): + return True + + return False + + + def getproxies_macosx_sysconf(): + """Return a dictionary of scheme -> proxy server URL mappings. + + This function uses the MacOSX framework SystemConfiguration + to fetch the proxy information. + """ + return _get_proxies() + - try: - config = ic.IC() - except ic.error: - return {} - proxies = {} - # HTTP: - if 'UseHTTPProxy' in config and config['UseHTTPProxy']: - try: - value = config['HTTPProxyHost'] - except ic.error: - pass - else: - proxies['http'] = 'http://%s' % value - # FTP: XXX To be done. - # Gopher: XXX To be done. - return proxies def proxy_bypass(host): if getproxies_environment(): return proxy_bypass_environment(host) else: - return 0 + return proxy_bypass_macosx_sysconf(host) def getproxies(): - return getproxies_environment() or getproxies_internetconfig() + return getproxies_environment() or getproxies_macosx_sysconf() + elif os.name == 'nt': def getproxies_registry(): |