@@ -2571,6 +2571,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
2571
2571
}
2572
2572
"""
2573
2573
from fnmatch import fnmatch
2574
+ from ipaddress import AddressValueError , IPv4Address
2574
2575
2575
2576
hostonly , port = _splitport (host )
2576
2577
@@ -2587,20 +2588,17 @@ def ip2num(ipAddr):
2587
2588
return True
2588
2589
2589
2590
hostIP = None
2591
+ try :
2592
+ hostIP = int (IPv4Address (hostonly ))
2593
+ except AddressValueError :
2594
+ pass
2590
2595
2591
2596
for value in proxy_settings .get ('exceptions' , ()):
2592
2597
# Items in the list are strings like these: *.local, 169.254/16
2593
2598
if not value : continue
2594
2599
2595
2600
m = re .match (r"(\d+(?:\.\d+)*)(/\d+)?" , value )
2596
- if m is not None :
2597
- if hostIP is None :
2598
- try :
2599
- hostIP = socket .gethostbyname (hostonly )
2600
- hostIP = ip2num (hostIP )
2601
- except OSError :
2602
- continue
2603
-
2601
+ if m is not None and hostIP is not None :
2604
2602
base = ip2num (m .group (1 ))
2605
2603
mask = m .group (2 )
2606
2604
if mask is None :
@@ -2623,6 +2621,31 @@ def ip2num(ipAddr):
2623
2621
return False
2624
2622
2625
2623
2624
+ # Same as _proxy_bypass_macosx_sysconf, testable on all platforms
2625
+ def _proxy_bypass_winreg_override (host , override ):
2626
+ """Return True if the host should bypass the proxy server.
2627
+
2628
+ The proxy override list is obtained from the Windows
2629
+ Internet settings proxy override registry value.
2630
+
2631
+ An example of a proxy override value is:
2632
+ "www.example.com;*.example.net; 192.168.0.1"
2633
+ """
2634
+ from fnmatch import fnmatch
2635
+
2636
+ host , _ = _splitport (host )
2637
+ proxy_override = override .split (';' )
2638
+ for test in proxy_override :
2639
+ test = test .strip ()
2640
+ # "<local>" should bypass the proxy server for all intranet addresses
2641
+ if test == '<local>' :
2642
+ if '.' not in host :
2643
+ return True
2644
+ elif fnmatch (host , test ):
2645
+ return True
2646
+ return False
2647
+
2648
+
2626
2649
if sys .platform == 'darwin' :
2627
2650
from _scproxy import _get_proxy_settings , _get_proxies
2628
2651
@@ -2721,7 +2744,7 @@ def proxy_bypass_registry(host):
2721
2744
import winreg
2722
2745
except ImportError :
2723
2746
# Std modules, so should be around - but you never know!
2724
- return 0
2747
+ return False
2725
2748
try :
2726
2749
internetSettings = winreg .OpenKey (winreg .HKEY_CURRENT_USER ,
2727
2750
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' )
@@ -2731,40 +2754,10 @@ def proxy_bypass_registry(host):
2731
2754
'ProxyOverride' )[0 ])
2732
2755
# ^^^^ Returned as Unicode but problems if not converted to ASCII
2733
2756
except OSError :
2734
- return 0
2757
+ return False
2735
2758
if not proxyEnable or not proxyOverride :
2736
- return 0
2737
- # try to make a host list from name and IP address.
2738
- rawHost , port = _splitport (host )
2739
- host = [rawHost ]
2740
- try :
2741
- addr = socket .gethostbyname (rawHost )
2742
- if addr != rawHost :
2743
- host .append (addr )
2744
- except OSError :
2745
- pass
2746
- try :
2747
- fqdn = socket .getfqdn (rawHost )
2748
- if fqdn != rawHost :
2749
- host .append (fqdn )
2750
- except OSError :
2751
- pass
2752
- # make a check value list from the registry entry: replace the
2753
- # '<local>' string by the localhost entry and the corresponding
2754
- # canonical entry.
2755
- proxyOverride = proxyOverride .split (';' )
2756
- # now check if we match one of the registry values.
2757
- for test in proxyOverride :
2758
- if test == '<local>' :
2759
- if '.' not in rawHost :
2760
- return 1
2761
- test = test .replace ("." , r"\." ) # mask dots
2762
- test = test .replace ("*" , r".*" ) # change glob sequence
2763
- test = test .replace ("?" , r"." ) # change glob char
2764
- for val in host :
2765
- if re .match (test , val , re .I ):
2766
- return 1
2767
- return 0
2759
+ return False
2760
+ return _proxy_bypass_winreg_override (host , proxyOverride )
2768
2761
2769
2762
def proxy_bypass (host ):
2770
2763
"""Return True, if host should be bypassed.
0 commit comments