本文整理汇总了Python中netaddr.IPAddress.is_netmask方法的典型用法代码示例。如果您正苦于以下问题:Python IPAddress.is_netmask方法的具体用法?Python IPAddress.is_netmask怎么用?Python IPAddress.is_netmask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.IPAddress
的用法示例。
在下文中一共展示了IPAddress.is_netmask方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()):
"""Encodes the specified data into an IPAddress object
:param data: the data to encode into an IPAddress
:type data: str or raw bytes (BBBB)
:return: the encoded IPAddress
"""
if isinstance(data, (str, int)):
try:
ip = IPAddress(data)
if ip is not None and ip.version == 4 and not ip.is_netmask():
return ip
except:
pass
try:
structFormat = ">"
if endianness == AbstractType.ENDIAN_BIG:
structFormat = ">"
if not sign == AbstractType.SIGN_SIGNED:
structFormat += "bbbb"
else:
structFormat += "BBBB"
quads = map(str, struct.unpack(structFormat, data))
strIP = string.join(quads, '.')
ip = IPAddress(strIP)
if ip is not None and ip.version == 4 and not ip.is_netmask():
return ip
except Exception, e:
raise TypeError("Impossible encode {0} into an IPv4 data ({1})".format(data, e))
示例2: validate_ipv4_address
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
def validate_ipv4_address(cls, _, value):
"""
Ensures the :attr:`ip` address is valid. This checks to ensure
that the value provided is:
* not a hostmask
* not link local (:rfc:`3927`)
* not used for multicast (:rfc:`1112`)
* not a netmask (:rfc:`4632`)
* not reserved (:rfc:`6052`)
* a private address (:rfc:`1918`)
"""
if value is None:
return value
try:
address = IPAddress(value)
except (AddrFormatError, ValueError) as e:
raise ValueError(
"%s is not a valid address format: %s" % (value, e))
if ALLOW_AGENT_LOOPBACK:
loopback = lambda: False
else:
loopback = address.is_loopback
if any([address.is_hostmask(), address.is_link_local(),
loopback(), address.is_multicast(),
address.is_netmask(), address.is_reserved()]):
raise ValueError("%s is not a valid address type" % value)
return value
示例3: is_valid_netmask
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
def is_valid_netmask(ip_addr):
"""Valid the format of a netmask"""
try:
ip_address = IPAddress(ip_addr)
return ip_address.is_netmask()
except Exception:
return False
示例4: ipv4_to_cidr
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
def ipv4_to_cidr(address, netmask=None, prefixlen=None):
a = IPAddress(address)
n = IPNetwork(a)
if netmask:
assert prefixlen is None, 'Cannot provide both netmask and prefixlen'
m = IPAddress(netmask)
assert m.is_netmask(), 'A valid netmask is required'
n.prefixlen = m.netmask_bits()
else:
assert prefixlen, 'Provide either netmask or prefixlen'
n.prefixlen = int(prefixlen)
return str(n.cidr)
示例5: IPNetmaskValidator
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
class IPNetmaskValidator(Validator):
def validate(self, value):
"""Return a boolean if the value is a valid netmask."""
try:
self._choice = IPAddress(value)
except AddrFormatError:
self.error_message = '%s is not a valid IP address.' % value
return False
if self._choice.is_netmask():
return True
else:
self.error_message = '%s is not a valid IP netmask.' % value
return False
示例6: ping
# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import is_netmask [as 别名]
def ping(self, targets=list(), filename=str(), status=str(), notDNS=False,
elapsed=False
):
"""
Attempt to ping a list of hosts or networks (can be a single host)
:param targets: List - Name(s) or IP(s) of the host(s).
:param filename: String - name of the file containing hosts to ping
:param status: String - if one of ['alive', 'dead', 'noip'] then only
return results that have that status. If this is not specified,
then all results will be returned.
:param notDNS: Bolean - If True turn on use of -A option for fping for
not use dns resolve names. Default is False.
:param elapsed: Bolean - If True turn on use of -e option for fping for
show elapsed time on return packets. Default is False.
:return: Type and results depends on whether status is specified:
if status == '': return dict: {targets: results}
if status != '': return list: targets if targets == status
"""
if targets and filename:
raise SyntaxError("You must specify only one of either targets=[] "
"or filename=''.")
elif not targets and not filename:
raise SyntaxError("You must specify either a list of targets or "
"filename='', but not both.")
elif filename:
targets = self.read_file(filename)
my_targets = {'hosts': [], 'nets': []}
addresses = []
# Check for valid networks and add hosts and nets to my_targets
for target in targets:
# Targets may include networks in the format "network mask", or,
# a file could contain multiple hosts or IP's on a single line.
if len(target.split()) > 1:
target_items = target.split()
for item in target_items:
try:
ip = IPAddress(item)
# If it is an IPv4 address or mask put in in addresses
if ip.version == 4:
addresses.append(str(ip))
except AddrFormatError:
# IP Address not detected, so assume it's a host name
my_targets['hosts'].append(item)
except ValueError:
# CIDR network detected
net = IPNetwork(item)
# Make sure it is a CIDR address acceptable to fping
if net.ip.is_unicast() and net.version == 4 and \
net.netmask.netmask_bits() in range(8, 31):
my_targets['nets'].append(target_items[0])
else:
msg = str(str(net) + ':Only IPv4 unicast addresses'
' with bit masks\n '
' from 8 to 30 are supported.')
raise AttributeError(msg)
# Iterate over the IP strings in addresses
while len(addresses) > 1:
ip = IPAddress(addresses[0])
mask = IPAddress(addresses[1])
# Test to see if IP is unicast, and mask is an actual mask
if ip.is_unicast() and mask.is_netmask():
net = IPNetwork(str(ip) + '/' + str(
mask.netmask_bits()))
# Convert ip and mask to CIDR and remove from addresses
my_targets['nets'].append(str(net.cidr))
addresses.pop(0)
addresses.pop(0)
elif ip.is_unicast() and not ip.is_netmask():
# mask was not a mask so only remove IP and start over
my_targets['hosts'].append(str(ip))
addresses.pop(0)
# There could be one more item in addresses, so check it
if addresses:
ip = IPAddress(addresses[0])
if ip.is_unicast() and not ip.is_netmask():
my_targets['hosts'].append(addresses[0])
addresses.pop()
# target has only one item, so check it
else:
try:
ip = IPAddress(target)
if ip.version == 4 and ip.is_unicast() and \
not ip.is_netmask():
my_targets['hosts'].append(target)
else:
msg = str(target + 'Only IPv4 unicast addresses are '
'supported.')
raise AttributeError(msg)
except AddrFormatError:
# IP Address not detected, so assume it's a host name
my_targets['hosts'].append(target)
except ValueError:
# CIDR network detected
net = IPNetwork(target)
if net.ip.is_unicast() and net.version == 4 and \
net.netmask.netmask_bits() in range(8, 31):
my_targets['nets'].append(target)
#.........这里部分代码省略.........