我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用ipaddress.NetmaskValueError()。
def _validate_cidr(self, rule): """Validate the cidr block in a rule. Returns: True: Upon successful completion. Raises: SpinnakerSecurityGroupCreationFailed: CIDR definition is invalid or the network range is too wide. """ try: network = ipaddress.IPv4Network(rule['app']) except (ipaddress.NetmaskValueError, ValueError) as error: raise SpinnakerSecurityGroupCreationFailed(error) self.log.debug('Validating CIDR: %s', network.exploded) return True
def _validate_cidr_format(cidr): """Validate CIDR IP range :param str cidr: :return: :rtype: bool """ try: ipaddress.ip_network(cidr, strict=False) except (ValueError, ipaddress.AddressValueError, ipaddress.NetmaskValueError): return False if '/' not in cidr: return False if re.search('\s', cidr): return False return True
def get_network_object(prefix): u = prefix.decode('utf8') try: return ipaddress.IPv4Network(u, strict=False) except (ValueError, ipaddress.AddressValueError, ipaddress.NetmaskValueError): pass try: return ipaddress.IPv6Network(u, strict=False) except (ValueError, ipaddress.AddressValueError, ipaddress.NetmaskValueError): return None
def assertNetmaskError(self, details, *args): """Ensure a clean NetmaskValueError""" return self.assertCleanError(ipaddress.NetmaskValueError, details, *args)
def __call__(self, value): try: import ipaddress except ImportError: from gluon.contrib import ipaddr as ipaddress try: ip = ipaddress.IPv6Address(value.decode('utf-8')) ok = True except ipaddress.AddressValueError: return (value, translate(self.error_message)) if self.subnets: # iterate through self.subnets to see if value is a member ok = False if isinstance(self.subnets, str): self.subnets = [self.subnets] for network in self.subnets: try: ipnet = ipaddress.IPv6Network(network.decode('utf-8')) except (ipaddress.NetmaskValueError, ipaddress.AddressValueError): return (value, translate('invalid subnet provided')) if ip in ipnet: ok = True if self.is_routeable: self.is_private = False self.is_link_local = False self.is_reserved = False self.is_multicast = False if not (self.is_private is None or self.is_private == ip.is_private): ok = False if not (self.is_link_local is None or self.is_link_local == ip.is_link_local): ok = False if not (self.is_reserved is None or self.is_reserved == ip.is_reserved): ok = False if not (self.is_multicast is None or self.is_multicast == ip.is_multicast): ok = False if not (self.is_6to4 is None or self.is_6to4 == ip.is_6to4): ok = False if not (self.is_teredo is None or self.is_teredo == ip.is_teredo): ok = False if ok: return (value, None) return (value, translate(self.error_message))
def __call__(self, value): try: import ipaddress except ImportError: from contrib import ipaddr as ipaddress try: ip = ipaddress.IPv6Address(value) ok = True except ipaddress.AddressValueError: return (value, translate(self.error_message)) if self.subnets: # iterate through self.subnets to see if value is a member ok = False if isinstance(self.subnets, str): self.subnets = [self.subnets] for network in self.subnets: try: ipnet = ipaddress.IPv6Network(network) except (ipaddress.NetmaskValueError, ipaddress.AddressValueError): return (value, translate('invalid subnet provided')) if ip in ipnet: ok = True if self.is_routeable: self.is_private = False self.is_link_local = False self.is_reserved = False self.is_multicast = False if not (self.is_private is None or self.is_private == ip.is_private): ok = False if not (self.is_link_local is None or self.is_link_local == ip.is_link_local): ok = False if not (self.is_reserved is None or self.is_reserved == ip.is_reserved): ok = False if not (self.is_multicast is None or self.is_multicast == ip.is_multicast): ok = False if not (self.is_6to4 is None or self.is_6to4 == ip.is_6to4): ok = False if not (self.is_teredo is None or self.is_teredo == ip.is_teredo): ok = False if ok: return (value, None) return (value, translate(self.error_message))
def __call__(self, value): try: import ipaddress except ImportError: from gluon.contrib import ipaddr as ipaddress try: ip = ipaddress.IPv6Address(value) ok = True except ipaddress.AddressValueError: return (value, translate(self.error_message)) if self.subnets: # iterate through self.subnets to see if value is a member ok = False if isinstance(self.subnets, str): self.subnets = [self.subnets] for network in self.subnets: try: ipnet = ipaddress.IPv6Network(network) except (ipaddress.NetmaskValueError, ipaddress.AddressValueError): return (value, translate('invalid subnet provided')) if ip in ipnet: ok = True if self.is_routeable: self.is_private = False self.is_link_local = False self.is_reserved = False self.is_multicast = False if not (self.is_private is None or self.is_private == ip.is_private): ok = False if not (self.is_link_local is None or self.is_link_local == ip.is_link_local): ok = False if not (self.is_reserved is None or self.is_reserved == ip.is_reserved): ok = False if not (self.is_multicast is None or self.is_multicast == ip.is_multicast): ok = False if not (self.is_6to4 is None or self.is_6to4 == ip.is_6to4): ok = False if not (self.is_teredo is None or self.is_teredo == ip.is_teredo): ok = False if ok: return (value, None) return (value, translate(self.error_message))