本文整理汇总了Python中netaddr.core.AddrFormatError方法的典型用法代码示例。如果您正苦于以下问题:Python core.AddrFormatError方法的具体用法?Python core.AddrFormatError怎么用?Python core.AddrFormatError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.core
的用法示例。
在下文中一共展示了core.AddrFormatError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: in_network
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def in_network(ip_address, cidrs):
"""Check that an ip_address is within a set of CIDRs
Args:
ip_address (str or netaddr.IPAddress): IP address to check
cidrs (set): String CIDRs
Returns:
Boolean representing if the given IP is within any CIDRs
"""
if not valid_ip(ip_address):
return False
for cidr in cidrs:
try:
network = IPNetwork(cidr)
except AddrFormatError:
LOGGER.error('Invalid IP Network: %s', cidr)
continue
if ip_address in network:
return True
return False
示例2: base85_to_ipv6
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def base85_to_ipv6(addr):
"""
Convert a base 85 IPv6 address to its hexadecimal format.
"""
tokens = list(addr)
if len(tokens) != 20:
raise AddrFormatError('Invalid base 85 IPv6 address: %r' % addr)
result = 0
for i, num in enumerate(reversed(tokens)):
num = BASE_85_DICT[num]
result += (num * 85 ** i)
ip = IPAddress(result, 6)
return str(ip)
示例3: _generate_nmap_octet_ranges
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def _generate_nmap_octet_ranges(nmap_target_spec):
# Generate 4 lists containing all octets defined by a given nmap Target
# specification.
if not _is_str(nmap_target_spec):
raise TypeError('string expected, not %s' % type(nmap_target_spec))
if not nmap_target_spec:
raise ValueError('nmap target specification cannot be blank!')
tokens = nmap_target_spec.split('.')
if len(tokens) != 4:
raise AddrFormatError('invalid nmap range: %s' % nmap_target_spec)
return (_nmap_octet_target_values(tokens[0]),
_nmap_octet_target_values(tokens[1]),
_nmap_octet_target_values(tokens[2]),
_nmap_octet_target_values(tokens[3]))
示例4: str_to_int
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def str_to_int(addr, flags=0):
"""
:param addr: An IPv4 dotted decimal address in string form.
:param flags: decides which rules are applied to the interpretation of the
addr value. Supported constants are INET_PTON and ZEROFILL. See the
netaddr.core docs for details.
:return: The equivalent unsigned integer for a given IPv4 address.
"""
if flags & ZEROFILL:
addr = '.'.join(['%d' % int(i) for i in addr.split('.')])
try:
if flags & INET_PTON:
return _struct.unpack('>I', _inet_pton(AF_INET, addr))[0]
else:
return _struct.unpack('>I', _inet_aton(addr))[0]
except Exception:
raise AddrFormatError('%r is not a valid IPv4 address string!' % addr)
示例5: valid_str
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def valid_str(addr, flags=0):
"""
:param addr: An IPv6 address in presentation (string) format.
:param flags: decides which rules are applied to the interpretation of the
addr value. Future use - currently has no effect.
:return: ``True`` if IPv6 address is valid, ``False`` otherwise.
"""
if addr == '':
raise AddrFormatError('Empty strings are not supported!')
try:
_inet_pton(AF_INET6, addr)
except:
return False
return True
示例6: base85_to_ipv6
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def base85_to_ipv6(addr):
"""
Convert a base 85 IPv6 address to its hexadecimal format.
"""
tokens = list(addr)
if len(tokens) != 20:
raise AddrFormatError('Invalid base 85 IPv6 addess: %r' % addr)
result = 0
for i, num in enumerate(reversed(tokens)):
num = BASE_85_DICT[num]
result += (num * 85 ** i)
ip = IPAddress(result, 6)
return str(ip)
示例7: _generate_nmap_octet_ranges
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def _generate_nmap_octet_ranges(nmap_target_spec):
# Generate 4 lists containing all octets defined by a given nmap Target
# specification.
if not _is_str(nmap_target_spec):
raise TypeError('string expected, not %s' % type(nmap_target_spec))
if not nmap_target_spec:
raise ValueError('nmap target specification cannot be blank!')
tokens = nmap_target_spec.split('.')
if len(tokens) != 4:
raise AddrFormatError('invalid nmap range: %s' % nmap_target_spec)
if tokens[0] == '-':
raise AddrFormatError('first octet cannot be a sole hyphen!')
return (_nmap_octet_target_values(tokens[0]),
_nmap_octet_target_values(tokens[1]),
_nmap_octet_target_values(tokens[2]),
_nmap_octet_target_values(tokens[3]))
#-----------------------------------------------------------------------------
示例8: str_to_int
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def str_to_int(addr, flags=0):
"""
:param addr: An IPv4 dotted decimal address in string form.
:param flags: decides which rules are applied to the interpretation of the
addr value. Supported constants are INET_PTON and ZEROFILL. See the
netaddr.core docs for details.
:return: The equivalent unsigned integer for a given IPv4 address.
"""
if flags & ZEROFILL:
addr = '.'.join(['%d' % int(i) for i in addr.split('.')])
try:
if flags & INET_PTON:
return _struct.unpack('>I', _inet_pton(AF_INET, addr))[0]
else:
return _struct.unpack('>I', _inet_aton(addr))[0]
except:
raise AddrFormatError('%r is not a valid IPv4 address string!' % addr)
#-----------------------------------------------------------------------------
示例9: valid_str
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def valid_str(addr, flags=0):
"""
:param addr: An IPv6 address in presentation (string) format.
:param flags: decides which rules are applied to the interpretation of the
addr value. Future use - currently has no effect.
:return: ``True`` if IPv6 address is valid, ``False`` otherwise.
"""
if addr == '':
raise AddrFormatError('Empty strings are not supported!')
try:
_inet_pton(AF_INET6, addr)
except:
return False
return True
#-----------------------------------------------------------------------------
示例10: __init__
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def __init__(self, params):
BasePayload.__init__(self, params)
try:
from netaddr import IPRange
from netaddr.core import AddrFormatError
ran = self.params["iprange"].split("-")
net = IPRange(ran[0], ran[1])
self.f = iter(net)
self.__count = net.size
except ImportError:
raise FuzzExceptBadInstall("ipnet plugin requires netaddr module. Please install it using pip.")
except AddrFormatError:
raise FuzzExceptPluginBadParams("The specified network range has an incorrect format.")
except IndexError:
raise FuzzExceptPluginBadParams("The specified network range has an incorrect format.")
示例11: __init__
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def __init__(self, params):
BasePayload.__init__(self, params)
try:
from netaddr import IPNetwork
from netaddr.core import AddrFormatError
net = IPNetwork('%s' % self.params["net"])
self.f = net.iter_hosts()
self.__count = net.size - 2
if self.__count <= 0:
raise FuzzExceptPluginBadParams("There are not hosts in the specified network")
except ValueError:
raise FuzzExceptPluginBadParams("The specified network has an incorrect format.")
except ImportError:
raise FuzzExceptBadInstall("ipnet plugin requires netaddr module. Please install it using pip.")
except AddrFormatError:
raise FuzzExceptPluginBadParams("The specified network has an incorrect format.")
示例12: resolve_host
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def resolve_host(system):
parsed = urlparse(system)
system = parsed.path if parsed.netloc == '' else parsed.netloc
try:
toresolve = IPAddress(system)
resolved = socket.gethostbyaddr(str(toresolve))[0]
return resolved
except AddrFormatError:
pass
except socket.herror:
return 'Unknown'
try:
resolved = socket.gethostbyname(system)
return resolved
except socket.gaierror:
return 'Unknown'
示例13: make_network
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def make_network(
ip_address: MaybeIPAddress, netmask_or_bits: int, cidr=False, **kwargs
) -> IPNetwork:
"""Construct an `IPNetwork` with the given address and netmask or width.
This is a thin wrapper for the `IPNetwork` constructor. It's here because
the constructor for `IPNetwork` is easy to get wrong. If you pass it an
IP address and a netmask, or an IP address and a bit size, it will seem to
work... but it will pick a default netmask, not the one you specified.
:param ip_address:
:param netmask_or_bits:
:param kwargs: Any other (keyword) arguments you want to pass to the
`IPNetwork` constructor.
:raise netaddr.core.AddrFormatError: If the network specification is
malformed.
:return: An `IPNetwork` of the given base address and netmask or bit width.
"""
network = IPNetwork("%s/%s" % (ip_address, netmask_or_bits), **kwargs)
if cidr:
network = network.cidr
return network
示例14: is_loopback_address
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def is_loopback_address(hostname):
"""Determine if the given hostname appears to be a loopback address.
:param hostname: either a hostname or an IP address. No resolution is
done, but 'localhost' is considered to be loopback.
:type hostname: str
:return: True if the address is a loopback address.
"""
try:
ip = IPAddress(hostname)
except AddrFormatError:
return hostname.lower() in {"localhost", "localhost."}
return ip.is_loopback() or (
ip.is_ipv4_mapped() and ip.ipv4().is_loopback()
)
示例15: clean_ip
# 需要导入模块: from netaddr import core [as 别名]
# 或者: from netaddr.core import AddrFormatError [as 别名]
def clean_ip(self, ipaddr):
"""Process one IP address (id or address) and return the id."""
# If it's a simple number, then assume it's already an id.
# If it's an IPAddress, then look up the id.
# Otherwise, just return the input, which is likely to result in an
# error later.
if isinstance(ipaddr, int):
return int(ipaddr)
elif isinstance(ipaddr, str) and ipaddr.isdigit():
return int(ipaddr)
elif isinstance(ipaddr, StaticIPAddress):
# In Django 1.11, instead of getting an object ID, Django will
# pre-adapt it to a StaticIPAddress.
return ipaddr.id
try:
IPAddress(ipaddr)
except (AddrFormatError, ValueError):
# We have no idea, pass it on through and see what happens.
return ipaddr
ips = StaticIPAddress.objects.filter(ip=ipaddr)
if ips.count() > 0:
return ips.first().id
return ipaddr