Skip to content

Commit 991d81d

Browse files
committed
Fix utils.is_valid_cidr
1 parent a814098 commit 991d81d

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/requests/utils.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -714,22 +714,11 @@ def is_valid_cidr(string_network):
714714
715715
:rtype: bool
716716
"""
717-
if string_network.count("/") == 1:
718-
try:
719-
mask = int(string_network.split("/")[1])
720-
except ValueError:
721-
return False
722-
723-
if mask < 1 or mask > 32:
724-
return False
725-
726-
try:
727-
socket.inet_aton(string_network.split("/")[0])
728-
except OSError:
729-
return False
730-
else:
717+
try:
718+
interface = ipaddress.ip_interface(string_network)
719+
except (ipaddress.AddressValueError, ValueError):
731720
return False
732-
return True
721+
return string_network in (interface.compressed, interface.exploded)
733722

734723

735724
@contextlib.contextmanager

0 commit comments

Comments
 (0)