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