from .net_ipv4_parse_ipv4 import parse_ipv4 def netmask_to_prefix(netmask: str) -> int: """The prefix length of a dotted-quad mask; a mask with a gap in its one bits is refused.""" value = parse_ipv4(netmask) host_part = 4294967295 - value + 1 for prefix in range(33): if 2 ** (32 - prefix) == host_part: return prefix raise ValueError('"%s" is not a contiguous netmask' % (netmask,))