@@ -2589,6 +2589,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
2589
2589
}
2590
2590
"""
2591
2591
from fnmatch import fnmatch
2592
+ from ipaddress import AddressValueError , IPv4Address
2592
2593
2593
2594
hostonly , port = _splitport (host )
2594
2595
@@ -2605,20 +2606,17 @@ def ip2num(ipAddr):
2605
2606
return True
2606
2607
2607
2608
hostIP = None
2609
+ try :
2610
+ hostIP = int (IPv4Address (hostonly ))
2611
+ except AddressValueError :
2612
+ pass
2608
2613
2609
2614
for value in proxy_settings .get ('exceptions' , ()):
2610
2615
# Items in the list are strings like these: *.local, 169.254/16
2611
2616
if not value : continue
2612
2617
2613
2618
m = re .match (r"(\d+(?:\.\d+)*)(/\d+)?" , value )
2614
- if m is not None :
2615
- if hostIP is None :
2616
- try :
2617
- hostIP = socket .gethostbyname (hostonly )
2618
- hostIP = ip2num (hostIP )
2619
- except OSError :
2620
- continue
2621
-
2619
+ if m is not None and hostIP is not None :
2622
2620
base = ip2num (m .group (1 ))
2623
2621
mask = m .group (2 )
2624
2622
if mask is None :
@@ -2641,6 +2639,31 @@ def ip2num(ipAddr):
2641
2639
return False
2642
2640
2643
2641
2642
+ # Same as _proxy_bypass_macosx_sysconf, testable on all platforms
2643
+ def _proxy_bypass_winreg_override (host , override ):
2644
+ """Return True if the host should bypass the proxy server.
2645
+
2646
+ The proxy override list is obtained from the Windows
2647
+ Internet settings proxy override registry value.
2648
+
2649
+ An example of a proxy override value is:
2650
+ "www.example.com;*.example.net; 192.168.0.1"
2651
+ """
2652
+ from fnmatch import fnmatch
2653
+
2654
+ host , _ = _splitport (host )
2655
+ proxy_override = override .split (';' )
2656
+ for test in proxy_override :
2657
+ test = test .strip ()
2658
+ # "<local>" should bypass the proxy server for all intranet addresses
2659
+ if test == '<local>' :
2660
+ if '.' not in host :
2661
+ return True
2662
+ elif fnmatch (host , test ):
2663
+ return True
2664
+ return False
2665
+
2666
+
2644
2667
if sys .platform == 'darwin' :
2645
2668
from _scproxy import _get_proxy_settings , _get_proxies
2646
2669
@@ -2739,7 +2762,7 @@ def proxy_bypass_registry(host):
2739
2762
import winreg
2740
2763
except ImportError :
2741
2764
# Std modules, so should be around - but you never know!
2742
- return 0
2765
+ return False
2743
2766
try :
2744
2767
internetSettings = winreg .OpenKey (winreg .HKEY_CURRENT_USER ,
2745
2768
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' )
@@ -2749,40 +2772,10 @@ def proxy_bypass_registry(host):
2749
2772
'ProxyOverride' )[0 ])
2750
2773
# ^^^^ Returned as Unicode but problems if not converted to ASCII
2751
2774
except OSError :
2752
- return 0
2775
+ return False
2753
2776
if not proxyEnable or not proxyOverride :
2754
- return 0
2755
- # try to make a host list from name and IP address.
2756
- rawHost , port = _splitport (host )
2757
- host = [rawHost ]
2758
- try :
2759
- addr = socket .gethostbyname (rawHost )
2760
- if addr != rawHost :
2761
- host .append (addr )
2762
- except OSError :
2763
- pass
2764
- try :
2765
- fqdn = socket .getfqdn (rawHost )
2766
- if fqdn != rawHost :
2767
- host .append (fqdn )
2768
- except OSError :
2769
- pass
2770
- # make a check value list from the registry entry: replace the
2771
- # '<local>' string by the localhost entry and the corresponding
2772
- # canonical entry.
2773
- proxyOverride = proxyOverride .split (';' )
2774
- # now check if we match one of the registry values.
2775
- for test in proxyOverride :
2776
- if test == '<local>' :
2777
- if '.' not in rawHost :
2778
- return 1
2779
- test = test .replace ("." , r"\." ) # mask dots
2780
- test = test .replace ("*" , r".*" ) # change glob sequence
2781
- test = test .replace ("?" , r"." ) # change glob char
2782
- for val in host :
2783
- if re .match (test , val , re .I ):
2784
- return 1
2785
- return 0
2777
+ return False
2778
+ return _proxy_bypass_winreg_override (host , proxyOverride )
2786
2779
2787
2780
def proxy_bypass (host ):
2788
2781
"""Return True, if host should be bypassed.
0 commit comments