@@ -2564,6 +2564,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
25642564 }
25652565 """
25662566 from fnmatch import fnmatch
2567+ from ipaddress import AddressValueError , IPv4Address
25672568
25682569 hostonly , port = _splitport (host )
25692570
@@ -2580,20 +2581,17 @@ def ip2num(ipAddr):
25802581 return True
25812582
25822583 hostIP = None
2584+ try :
2585+ hostIP = int (IPv4Address (hostonly ))
2586+ except AddressValueError :
2587+ pass
25832588
25842589 for value in proxy_settings .get ('exceptions' , ()):
25852590 # Items in the list are strings like these: *.local, 169.254/16
25862591 if not value : continue
25872592
25882593 m = re .match (r"(\d+(?:\.\d+)*)(/\d+)?" , value )
2589- if m is not None :
2590- if hostIP is None :
2591- try :
2592- hostIP = socket .gethostbyname (hostonly )
2593- hostIP = ip2num (hostIP )
2594- except OSError :
2595- continue
2596-
2594+ if m is not None and hostIP is not None :
25972595 base = ip2num (m .group (1 ))
25982596 mask = m .group (2 )
25992597 if mask is None :
@@ -2616,6 +2614,31 @@ def ip2num(ipAddr):
26162614 return False
26172615
26182616
2617+ # Same as _proxy_bypass_macosx_sysconf, testable on all platforms
2618+ def _proxy_bypass_winreg_override (host , override ):
2619+ """Return True if the host should bypass the proxy server.
2620+
2621+ The proxy override list is obtained from the Windows
2622+ Internet settings proxy override registry value.
2623+
2624+ An example of a proxy override value is:
2625+ "www.example.com;*.example.net; 192.168.0.1"
2626+ """
2627+ from fnmatch import fnmatch
2628+
2629+ host , _ = _splitport (host )
2630+ proxy_override = override .split (';' )
2631+ for test in proxy_override :
2632+ test = test .strip ()
2633+ # "<local>" should bypass the proxy server for all intranet addresses
2634+ if test == '<local>' :
2635+ if '.' not in host :
2636+ return True
2637+ elif fnmatch (host , test ):
2638+ return True
2639+ return False
2640+
2641+
26192642if sys .platform == 'darwin' :
26202643 from _scproxy import _get_proxy_settings , _get_proxies
26212644
@@ -2714,7 +2737,7 @@ def proxy_bypass_registry(host):
27142737 import winreg
27152738 except ImportError :
27162739 # Std modules, so should be around - but you never know!
2717- return 0
2740+ return False
27182741 try :
27192742 internetSettings = winreg .OpenKey (winreg .HKEY_CURRENT_USER ,
27202743 r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' )
@@ -2724,40 +2747,10 @@ def proxy_bypass_registry(host):
27242747 'ProxyOverride' )[0 ])
27252748 # ^^^^ Returned as Unicode but problems if not converted to ASCII
27262749 except OSError :
2727- return 0
2750+ return False
27282751 if not proxyEnable or not proxyOverride :
2729- return 0
2730- # try to make a host list from name and IP address.
2731- rawHost , port = _splitport (host )
2732- host = [rawHost ]
2733- try :
2734- addr = socket .gethostbyname (rawHost )
2735- if addr != rawHost :
2736- host .append (addr )
2737- except OSError :
2738- pass
2739- try :
2740- fqdn = socket .getfqdn (rawHost )
2741- if fqdn != rawHost :
2742- host .append (fqdn )
2743- except OSError :
2744- pass
2745- # make a check value list from the registry entry: replace the
2746- # '<local>' string by the localhost entry and the corresponding
2747- # canonical entry.
2748- proxyOverride = proxyOverride .split (';' )
2749- # now check if we match one of the registry values.
2750- for test in proxyOverride :
2751- if test == '<local>' :
2752- if '.' not in rawHost :
2753- return 1
2754- test = test .replace ("." , r"\." ) # mask dots
2755- test = test .replace ("*" , r".*" ) # change glob sequence
2756- test = test .replace ("?" , r"." ) # change glob char
2757- for val in host :
2758- if re .match (test , val , re .I ):
2759- return 1
2760- return 0
2752+ return False
2753+ return _proxy_bypass_winreg_override (host , proxyOverride )
27612754
27622755 def proxy_bypass (host ):
27632756 """Return True, if host should be bypassed.
0 commit comments