def is_valid_cidr(cidr):
"""Verifies that a string is valid IPv4 or IPv6 CIDR specification.
A cleaned up version of the CIDR string is returned if it is verified,
otherwise a false value is returned.
Uses the IPy library to verify addresses.
"""
if (isinstance(cidr, six.string_types) and
not cidr.isdigit() and '/' in cidr):
try:
valid_cidr = IPy.IP(cidr) is not None
except (ValueError, TypeError):
return False
else:
return valid_cidr
return False
评论列表
文章目录