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)
评论列表
文章目录