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