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