@@ -2564,6 +2564,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
2564
2564
}
2565
2565
"""
2566
2566
from fnmatch import fnmatch
2567
+ from ipaddress import AddressValueError , IPv4Address
2567
2568
2568
2569
hostonly , port = _splitport (host )
2569
2570
@@ -2580,20 +2581,17 @@ def ip2num(ipAddr):
2580
2581
return True
2581
2582
2582
2583
hostIP = None
2584
+ try :
2585
+ hostIP = int (IPv4Address (hostonly ))
2586
+ except AddressValueError :
2587
+ pass
2583
2588
2584
2589
for value in proxy_settings .get ('exceptions' , ()):
2585
2590
# Items in the list are strings like these: *.local, 169.254/16
2586
2591
if not value : continue
2587
2592
2588
2593
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 :
2597
2595
base = ip2num (m .group (1 ))
2598
2596
mask = m .group (2 )
2599
2597
if mask is None :
@@ -2616,6 +2614,31 @@ def ip2num(ipAddr):
2616
2614
return False
2617
2615
2618
2616
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
+
2619
2642
if sys .platform == 'darwin' :
2620
2643
from _scproxy import _get_proxy_settings , _get_proxies
2621
2644
@@ -2714,7 +2737,7 @@ def proxy_bypass_registry(host):
2714
2737
import winreg
2715
2738
except ImportError :
2716
2739
# Std modules, so should be around - but you never know!
2717
- return 0
2740
+ return False
2718
2741
try :
2719
2742
internetSettings = winreg .OpenKey (winreg .HKEY_CURRENT_USER ,
2720
2743
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' )
@@ -2724,40 +2747,10 @@ def proxy_bypass_registry(host):
2724
2747
'ProxyOverride' )[0 ])
2725
2748
# ^^^^ Returned as Unicode but problems if not converted to ASCII
2726
2749
except OSError :
2727
- return 0
2750
+ return False
2728
2751
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 )
2761
2754
2762
2755
def proxy_bypass (host ):
2763
2756
"""Return True, if host should be bypassed.
0 commit comments