mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
function is_ipv4_network renamed
more detailed check of cidr format
This commit is contained in:
+12
-4
@@ -437,9 +437,17 @@ def is_ipv4_address(string_ip):
|
||||
return True
|
||||
|
||||
|
||||
def is_ipv4_network(string_network):
|
||||
"""Very simple check of the network format in no_proxy variable"""
|
||||
if '/' in string_network:
|
||||
def is_valid_cidr(string_network):
|
||||
"""Very simple check of the cidr format in no_proxy variable"""
|
||||
if string_network.count('/') == 1:
|
||||
try:
|
||||
mask = int(string_network.split('/')[1])
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
if mask < 1 or mask > 32:
|
||||
return False
|
||||
|
||||
try:
|
||||
socket.inet_aton(string_network.split('/')[0])
|
||||
except socket.error:
|
||||
@@ -467,7 +475,7 @@ def get_environ_proxies(url):
|
||||
ip = netloc.split(':')[0]
|
||||
if is_ipv4_address(ip):
|
||||
for proxy_ip in no_proxy:
|
||||
if is_ipv4_network(proxy_ip):
|
||||
if is_valid_cidr(proxy_ip):
|
||||
if address_in_network(ip, proxy_ip):
|
||||
return {}
|
||||
else:
|
||||
|
||||
+4
-4
@@ -934,10 +934,10 @@ class UtilsTestCase(unittest.TestCase):
|
||||
assert not is_ipv4_address('8.8.8.8.8')
|
||||
assert not is_ipv4_address('localhost.localdomain')
|
||||
|
||||
def test_is_ipv4_network(self):
|
||||
from requests.utils import is_ipv4_network
|
||||
assert not is_ipv4_network('8.8.8.8')
|
||||
assert is_ipv4_network('192.168.1.0/24')
|
||||
def test_is_valid_cidr(self):
|
||||
from requests.utils import is_valid_cidr
|
||||
assert not is_valid_cidr('8.8.8.8')
|
||||
assert is_valid_cidr('192.168.1.0/24')
|
||||
|
||||
def test_dotted_netmask(self):
|
||||
from requests.utils import dotted_netmask
|
||||
|
||||
Reference in New Issue
Block a user