Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: ipaddress.py
# Copyright 2007 Google Inc.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""A fast, lightweight IPv4/IPv6 manipulation library in Python.
[3] Fix | Delete
[4] Fix | Delete
This library is used to create/poke/manipulate IPv4 and IPv6 addresses
[5] Fix | Delete
and networks.
[6] Fix | Delete
[7] Fix | Delete
"""
[8] Fix | Delete
[9] Fix | Delete
__version__ = '1.0'
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
import functools
[13] Fix | Delete
[14] Fix | Delete
IPV4LENGTH = 32
[15] Fix | Delete
IPV6LENGTH = 128
[16] Fix | Delete
[17] Fix | Delete
class AddressValueError(ValueError):
[18] Fix | Delete
"""A Value Error related to the address."""
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class NetmaskValueError(ValueError):
[22] Fix | Delete
"""A Value Error related to the netmask."""
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
def ip_address(address):
[26] Fix | Delete
"""Take an IP string/int and return an object of the correct type.
[27] Fix | Delete
[28] Fix | Delete
Args:
[29] Fix | Delete
address: A string or integer, the IP address. Either IPv4 or
[30] Fix | Delete
IPv6 addresses may be supplied; integers less than 2**32 will
[31] Fix | Delete
be considered to be IPv4 by default.
[32] Fix | Delete
[33] Fix | Delete
Returns:
[34] Fix | Delete
An IPv4Address or IPv6Address object.
[35] Fix | Delete
[36] Fix | Delete
Raises:
[37] Fix | Delete
ValueError: if the *address* passed isn't either a v4 or a v6
[38] Fix | Delete
address
[39] Fix | Delete
[40] Fix | Delete
"""
[41] Fix | Delete
try:
[42] Fix | Delete
return IPv4Address(address)
[43] Fix | Delete
except (AddressValueError, NetmaskValueError):
[44] Fix | Delete
pass
[45] Fix | Delete
[46] Fix | Delete
try:
[47] Fix | Delete
return IPv6Address(address)
[48] Fix | Delete
except (AddressValueError, NetmaskValueError):
[49] Fix | Delete
pass
[50] Fix | Delete
[51] Fix | Delete
raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
[52] Fix | Delete
address)
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
def ip_network(address, strict=True):
[56] Fix | Delete
"""Take an IP string/int and return an object of the correct type.
[57] Fix | Delete
[58] Fix | Delete
Args:
[59] Fix | Delete
address: A string or integer, the IP network. Either IPv4 or
[60] Fix | Delete
IPv6 networks may be supplied; integers less than 2**32 will
[61] Fix | Delete
be considered to be IPv4 by default.
[62] Fix | Delete
[63] Fix | Delete
Returns:
[64] Fix | Delete
An IPv4Network or IPv6Network object.
[65] Fix | Delete
[66] Fix | Delete
Raises:
[67] Fix | Delete
ValueError: if the string passed isn't either a v4 or a v6
[68] Fix | Delete
address. Or if the network has host bits set.
[69] Fix | Delete
[70] Fix | Delete
"""
[71] Fix | Delete
try:
[72] Fix | Delete
return IPv4Network(address, strict)
[73] Fix | Delete
except (AddressValueError, NetmaskValueError):
[74] Fix | Delete
pass
[75] Fix | Delete
[76] Fix | Delete
try:
[77] Fix | Delete
return IPv6Network(address, strict)
[78] Fix | Delete
except (AddressValueError, NetmaskValueError):
[79] Fix | Delete
pass
[80] Fix | Delete
[81] Fix | Delete
raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %
[82] Fix | Delete
address)
[83] Fix | Delete
[84] Fix | Delete
[85] Fix | Delete
def ip_interface(address):
[86] Fix | Delete
"""Take an IP string/int and return an object of the correct type.
[87] Fix | Delete
[88] Fix | Delete
Args:
[89] Fix | Delete
address: A string or integer, the IP address. Either IPv4 or
[90] Fix | Delete
IPv6 addresses may be supplied; integers less than 2**32 will
[91] Fix | Delete
be considered to be IPv4 by default.
[92] Fix | Delete
[93] Fix | Delete
Returns:
[94] Fix | Delete
An IPv4Interface or IPv6Interface object.
[95] Fix | Delete
[96] Fix | Delete
Raises:
[97] Fix | Delete
ValueError: if the string passed isn't either a v4 or a v6
[98] Fix | Delete
address.
[99] Fix | Delete
[100] Fix | Delete
Notes:
[101] Fix | Delete
The IPv?Interface classes describe an Address on a particular
[102] Fix | Delete
Network, so they're basically a combination of both the Address
[103] Fix | Delete
and Network classes.
[104] Fix | Delete
[105] Fix | Delete
"""
[106] Fix | Delete
try:
[107] Fix | Delete
return IPv4Interface(address)
[108] Fix | Delete
except (AddressValueError, NetmaskValueError):
[109] Fix | Delete
pass
[110] Fix | Delete
[111] Fix | Delete
try:
[112] Fix | Delete
return IPv6Interface(address)
[113] Fix | Delete
except (AddressValueError, NetmaskValueError):
[114] Fix | Delete
pass
[115] Fix | Delete
[116] Fix | Delete
raise ValueError('%r does not appear to be an IPv4 or IPv6 interface' %
[117] Fix | Delete
address)
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
def v4_int_to_packed(address):
[121] Fix | Delete
"""Represent an address as 4 packed bytes in network (big-endian) order.
[122] Fix | Delete
[123] Fix | Delete
Args:
[124] Fix | Delete
address: An integer representation of an IPv4 IP address.
[125] Fix | Delete
[126] Fix | Delete
Returns:
[127] Fix | Delete
The integer address packed as 4 bytes in network (big-endian) order.
[128] Fix | Delete
[129] Fix | Delete
Raises:
[130] Fix | Delete
ValueError: If the integer is negative or too large to be an
[131] Fix | Delete
IPv4 IP address.
[132] Fix | Delete
[133] Fix | Delete
"""
[134] Fix | Delete
try:
[135] Fix | Delete
return address.to_bytes(4, 'big')
[136] Fix | Delete
except OverflowError:
[137] Fix | Delete
raise ValueError("Address negative or too large for IPv4")
[138] Fix | Delete
[139] Fix | Delete
[140] Fix | Delete
def v6_int_to_packed(address):
[141] Fix | Delete
"""Represent an address as 16 packed bytes in network (big-endian) order.
[142] Fix | Delete
[143] Fix | Delete
Args:
[144] Fix | Delete
address: An integer representation of an IPv6 IP address.
[145] Fix | Delete
[146] Fix | Delete
Returns:
[147] Fix | Delete
The integer address packed as 16 bytes in network (big-endian) order.
[148] Fix | Delete
[149] Fix | Delete
"""
[150] Fix | Delete
try:
[151] Fix | Delete
return address.to_bytes(16, 'big')
[152] Fix | Delete
except OverflowError:
[153] Fix | Delete
raise ValueError("Address negative or too large for IPv6")
[154] Fix | Delete
[155] Fix | Delete
[156] Fix | Delete
def _split_optional_netmask(address):
[157] Fix | Delete
"""Helper to split the netmask and raise AddressValueError if needed"""
[158] Fix | Delete
addr = str(address).split('/')
[159] Fix | Delete
if len(addr) > 2:
[160] Fix | Delete
raise AddressValueError("Only one '/' permitted in %r" % address)
[161] Fix | Delete
return addr
[162] Fix | Delete
[163] Fix | Delete
[164] Fix | Delete
def _find_address_range(addresses):
[165] Fix | Delete
"""Find a sequence of sorted deduplicated IPv#Address.
[166] Fix | Delete
[167] Fix | Delete
Args:
[168] Fix | Delete
addresses: a list of IPv#Address objects.
[169] Fix | Delete
[170] Fix | Delete
Yields:
[171] Fix | Delete
A tuple containing the first and last IP addresses in the sequence.
[172] Fix | Delete
[173] Fix | Delete
"""
[174] Fix | Delete
it = iter(addresses)
[175] Fix | Delete
first = last = next(it)
[176] Fix | Delete
for ip in it:
[177] Fix | Delete
if ip._ip != last._ip + 1:
[178] Fix | Delete
yield first, last
[179] Fix | Delete
first = ip
[180] Fix | Delete
last = ip
[181] Fix | Delete
yield first, last
[182] Fix | Delete
[183] Fix | Delete
[184] Fix | Delete
def _count_righthand_zero_bits(number, bits):
[185] Fix | Delete
"""Count the number of zero bits on the right hand side.
[186] Fix | Delete
[187] Fix | Delete
Args:
[188] Fix | Delete
number: an integer.
[189] Fix | Delete
bits: maximum number of bits to count.
[190] Fix | Delete
[191] Fix | Delete
Returns:
[192] Fix | Delete
The number of zero bits on the right hand side of the number.
[193] Fix | Delete
[194] Fix | Delete
"""
[195] Fix | Delete
if number == 0:
[196] Fix | Delete
return bits
[197] Fix | Delete
return min(bits, (~number & (number-1)).bit_length())
[198] Fix | Delete
[199] Fix | Delete
[200] Fix | Delete
def summarize_address_range(first, last):
[201] Fix | Delete
"""Summarize a network range given the first and last IP addresses.
[202] Fix | Delete
[203] Fix | Delete
Example:
[204] Fix | Delete
>>> list(summarize_address_range(IPv4Address('192.0.2.0'),
[205] Fix | Delete
... IPv4Address('192.0.2.130')))
[206] Fix | Delete
... #doctest: +NORMALIZE_WHITESPACE
[207] Fix | Delete
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'),
[208] Fix | Delete
IPv4Network('192.0.2.130/32')]
[209] Fix | Delete
[210] Fix | Delete
Args:
[211] Fix | Delete
first: the first IPv4Address or IPv6Address in the range.
[212] Fix | Delete
last: the last IPv4Address or IPv6Address in the range.
[213] Fix | Delete
[214] Fix | Delete
Returns:
[215] Fix | Delete
An iterator of the summarized IPv(4|6) network objects.
[216] Fix | Delete
[217] Fix | Delete
Raise:
[218] Fix | Delete
TypeError:
[219] Fix | Delete
If the first and last objects are not IP addresses.
[220] Fix | Delete
If the first and last objects are not the same version.
[221] Fix | Delete
ValueError:
[222] Fix | Delete
If the last object is not greater than the first.
[223] Fix | Delete
If the version of the first address is not 4 or 6.
[224] Fix | Delete
[225] Fix | Delete
"""
[226] Fix | Delete
if (not (isinstance(first, _BaseAddress) and
[227] Fix | Delete
isinstance(last, _BaseAddress))):
[228] Fix | Delete
raise TypeError('first and last must be IP addresses, not networks')
[229] Fix | Delete
if first.version != last.version:
[230] Fix | Delete
raise TypeError("%s and %s are not of the same version" % (
[231] Fix | Delete
first, last))
[232] Fix | Delete
if first > last:
[233] Fix | Delete
raise ValueError('last IP address must be greater than first')
[234] Fix | Delete
[235] Fix | Delete
if first.version == 4:
[236] Fix | Delete
ip = IPv4Network
[237] Fix | Delete
elif first.version == 6:
[238] Fix | Delete
ip = IPv6Network
[239] Fix | Delete
else:
[240] Fix | Delete
raise ValueError('unknown IP version')
[241] Fix | Delete
[242] Fix | Delete
ip_bits = first._max_prefixlen
[243] Fix | Delete
first_int = first._ip
[244] Fix | Delete
last_int = last._ip
[245] Fix | Delete
while first_int <= last_int:
[246] Fix | Delete
nbits = min(_count_righthand_zero_bits(first_int, ip_bits),
[247] Fix | Delete
(last_int - first_int + 1).bit_length() - 1)
[248] Fix | Delete
net = ip((first_int, ip_bits - nbits))
[249] Fix | Delete
yield net
[250] Fix | Delete
first_int += 1 << nbits
[251] Fix | Delete
if first_int - 1 == ip._ALL_ONES:
[252] Fix | Delete
break
[253] Fix | Delete
[254] Fix | Delete
[255] Fix | Delete
def _collapse_addresses_internal(addresses):
[256] Fix | Delete
"""Loops through the addresses, collapsing concurrent netblocks.
[257] Fix | Delete
[258] Fix | Delete
Example:
[259] Fix | Delete
[260] Fix | Delete
ip1 = IPv4Network('192.0.2.0/26')
[261] Fix | Delete
ip2 = IPv4Network('192.0.2.64/26')
[262] Fix | Delete
ip3 = IPv4Network('192.0.2.128/26')
[263] Fix | Delete
ip4 = IPv4Network('192.0.2.192/26')
[264] Fix | Delete
[265] Fix | Delete
_collapse_addresses_internal([ip1, ip2, ip3, ip4]) ->
[266] Fix | Delete
[IPv4Network('192.0.2.0/24')]
[267] Fix | Delete
[268] Fix | Delete
This shouldn't be called directly; it is called via
[269] Fix | Delete
collapse_addresses([]).
[270] Fix | Delete
[271] Fix | Delete
Args:
[272] Fix | Delete
addresses: A list of IPv4Network's or IPv6Network's
[273] Fix | Delete
[274] Fix | Delete
Returns:
[275] Fix | Delete
A list of IPv4Network's or IPv6Network's depending on what we were
[276] Fix | Delete
passed.
[277] Fix | Delete
[278] Fix | Delete
"""
[279] Fix | Delete
# First merge
[280] Fix | Delete
to_merge = list(addresses)
[281] Fix | Delete
subnets = {}
[282] Fix | Delete
while to_merge:
[283] Fix | Delete
net = to_merge.pop()
[284] Fix | Delete
supernet = net.supernet()
[285] Fix | Delete
existing = subnets.get(supernet)
[286] Fix | Delete
if existing is None:
[287] Fix | Delete
subnets[supernet] = net
[288] Fix | Delete
elif existing != net:
[289] Fix | Delete
# Merge consecutive subnets
[290] Fix | Delete
del subnets[supernet]
[291] Fix | Delete
to_merge.append(supernet)
[292] Fix | Delete
# Then iterate over resulting networks, skipping subsumed subnets
[293] Fix | Delete
last = None
[294] Fix | Delete
for net in sorted(subnets.values()):
[295] Fix | Delete
if last is not None:
[296] Fix | Delete
# Since they are sorted, last.network_address <= net.network_address
[297] Fix | Delete
# is a given.
[298] Fix | Delete
if last.broadcast_address >= net.broadcast_address:
[299] Fix | Delete
continue
[300] Fix | Delete
yield net
[301] Fix | Delete
last = net
[302] Fix | Delete
[303] Fix | Delete
[304] Fix | Delete
def collapse_addresses(addresses):
[305] Fix | Delete
"""Collapse a list of IP objects.
[306] Fix | Delete
[307] Fix | Delete
Example:
[308] Fix | Delete
collapse_addresses([IPv4Network('192.0.2.0/25'),
[309] Fix | Delete
IPv4Network('192.0.2.128/25')]) ->
[310] Fix | Delete
[IPv4Network('192.0.2.0/24')]
[311] Fix | Delete
[312] Fix | Delete
Args:
[313] Fix | Delete
addresses: An iterator of IPv4Network or IPv6Network objects.
[314] Fix | Delete
[315] Fix | Delete
Returns:
[316] Fix | Delete
An iterator of the collapsed IPv(4|6)Network objects.
[317] Fix | Delete
[318] Fix | Delete
Raises:
[319] Fix | Delete
TypeError: If passed a list of mixed version objects.
[320] Fix | Delete
[321] Fix | Delete
"""
[322] Fix | Delete
addrs = []
[323] Fix | Delete
ips = []
[324] Fix | Delete
nets = []
[325] Fix | Delete
[326] Fix | Delete
# split IP addresses and networks
[327] Fix | Delete
for ip in addresses:
[328] Fix | Delete
if isinstance(ip, _BaseAddress):
[329] Fix | Delete
if ips and ips[-1]._version != ip._version:
[330] Fix | Delete
raise TypeError("%s and %s are not of the same version" % (
[331] Fix | Delete
ip, ips[-1]))
[332] Fix | Delete
ips.append(ip)
[333] Fix | Delete
elif ip._prefixlen == ip._max_prefixlen:
[334] Fix | Delete
if ips and ips[-1]._version != ip._version:
[335] Fix | Delete
raise TypeError("%s and %s are not of the same version" % (
[336] Fix | Delete
ip, ips[-1]))
[337] Fix | Delete
try:
[338] Fix | Delete
ips.append(ip.ip)
[339] Fix | Delete
except AttributeError:
[340] Fix | Delete
ips.append(ip.network_address)
[341] Fix | Delete
else:
[342] Fix | Delete
if nets and nets[-1]._version != ip._version:
[343] Fix | Delete
raise TypeError("%s and %s are not of the same version" % (
[344] Fix | Delete
ip, nets[-1]))
[345] Fix | Delete
nets.append(ip)
[346] Fix | Delete
[347] Fix | Delete
# sort and dedup
[348] Fix | Delete
ips = sorted(set(ips))
[349] Fix | Delete
[350] Fix | Delete
# find consecutive address ranges in the sorted sequence and summarize them
[351] Fix | Delete
if ips:
[352] Fix | Delete
for first, last in _find_address_range(ips):
[353] Fix | Delete
addrs.extend(summarize_address_range(first, last))
[354] Fix | Delete
[355] Fix | Delete
return _collapse_addresses_internal(addrs + nets)
[356] Fix | Delete
[357] Fix | Delete
[358] Fix | Delete
def get_mixed_type_key(obj):
[359] Fix | Delete
"""Return a key suitable for sorting between networks and addresses.
[360] Fix | Delete
[361] Fix | Delete
Address and Network objects are not sortable by default; they're
[362] Fix | Delete
fundamentally different so the expression
[363] Fix | Delete
[364] Fix | Delete
IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
[365] Fix | Delete
[366] Fix | Delete
doesn't make any sense. There are some times however, where you may wish
[367] Fix | Delete
to have ipaddress sort these for you anyway. If you need to do this, you
[368] Fix | Delete
can use this function as the key= argument to sorted().
[369] Fix | Delete
[370] Fix | Delete
Args:
[371] Fix | Delete
obj: either a Network or Address object.
[372] Fix | Delete
Returns:
[373] Fix | Delete
appropriate key.
[374] Fix | Delete
[375] Fix | Delete
"""
[376] Fix | Delete
if isinstance(obj, _BaseNetwork):
[377] Fix | Delete
return obj._get_networks_key()
[378] Fix | Delete
elif isinstance(obj, _BaseAddress):
[379] Fix | Delete
return obj._get_address_key()
[380] Fix | Delete
return NotImplemented
[381] Fix | Delete
[382] Fix | Delete
[383] Fix | Delete
class _IPAddressBase:
[384] Fix | Delete
[385] Fix | Delete
"""The mother class."""
[386] Fix | Delete
[387] Fix | Delete
__slots__ = ()
[388] Fix | Delete
[389] Fix | Delete
@property
[390] Fix | Delete
def exploded(self):
[391] Fix | Delete
"""Return the longhand version of the IP address as a string."""
[392] Fix | Delete
return self._explode_shorthand_ip_string()
[393] Fix | Delete
[394] Fix | Delete
@property
[395] Fix | Delete
def compressed(self):
[396] Fix | Delete
"""Return the shorthand version of the IP address as a string."""
[397] Fix | Delete
return str(self)
[398] Fix | Delete
[399] Fix | Delete
@property
[400] Fix | Delete
def reverse_pointer(self):
[401] Fix | Delete
"""The name of the reverse DNS pointer for the IP address, e.g.:
[402] Fix | Delete
>>> ipaddress.ip_address("127.0.0.1").reverse_pointer
[403] Fix | Delete
'1.0.0.127.in-addr.arpa'
[404] Fix | Delete
>>> ipaddress.ip_address("2001:db8::1").reverse_pointer
[405] Fix | Delete
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'
[406] Fix | Delete
[407] Fix | Delete
"""
[408] Fix | Delete
return self._reverse_pointer()
[409] Fix | Delete
[410] Fix | Delete
@property
[411] Fix | Delete
def version(self):
[412] Fix | Delete
msg = '%200s has no version specified' % (type(self),)
[413] Fix | Delete
raise NotImplementedError(msg)
[414] Fix | Delete
[415] Fix | Delete
def _check_int_address(self, address):
[416] Fix | Delete
if address < 0:
[417] Fix | Delete
msg = "%d (< 0) is not permitted as an IPv%d address"
[418] Fix | Delete
raise AddressValueError(msg % (address, self._version))
[419] Fix | Delete
if address > self._ALL_ONES:
[420] Fix | Delete
msg = "%d (>= 2**%d) is not permitted as an IPv%d address"
[421] Fix | Delete
raise AddressValueError(msg % (address, self._max_prefixlen,
[422] Fix | Delete
self._version))
[423] Fix | Delete
[424] Fix | Delete
def _check_packed_address(self, address, expected_len):
[425] Fix | Delete
address_len = len(address)
[426] Fix | Delete
if address_len != expected_len:
[427] Fix | Delete
msg = "%r (len %d != %d) is not permitted as an IPv%d address"
[428] Fix | Delete
raise AddressValueError(msg % (address, address_len,
[429] Fix | Delete
expected_len, self._version))
[430] Fix | Delete
[431] Fix | Delete
@classmethod
[432] Fix | Delete
def _ip_int_from_prefix(cls, prefixlen):
[433] Fix | Delete
"""Turn the prefix length into a bitwise netmask
[434] Fix | Delete
[435] Fix | Delete
Args:
[436] Fix | Delete
prefixlen: An integer, the prefix length.
[437] Fix | Delete
[438] Fix | Delete
Returns:
[439] Fix | Delete
An integer.
[440] Fix | Delete
[441] Fix | Delete
"""
[442] Fix | Delete
return cls._ALL_ONES ^ (cls._ALL_ONES >> prefixlen)
[443] Fix | Delete
[444] Fix | Delete
@classmethod
[445] Fix | Delete
def _prefix_from_ip_int(cls, ip_int):
[446] Fix | Delete
"""Return prefix length from the bitwise netmask.
[447] Fix | Delete
[448] Fix | Delete
Args:
[449] Fix | Delete
ip_int: An integer, the netmask in expanded bitwise format
[450] Fix | Delete
[451] Fix | Delete
Returns:
[452] Fix | Delete
An integer, the prefix length.
[453] Fix | Delete
[454] Fix | Delete
Raises:
[455] Fix | Delete
ValueError: If the input intermingles zeroes & ones
[456] Fix | Delete
"""
[457] Fix | Delete
trailing_zeroes = _count_righthand_zero_bits(ip_int,
[458] Fix | Delete
cls._max_prefixlen)
[459] Fix | Delete
prefixlen = cls._max_prefixlen - trailing_zeroes
[460] Fix | Delete
leading_ones = ip_int >> trailing_zeroes
[461] Fix | Delete
all_ones = (1 << prefixlen) - 1
[462] Fix | Delete
if leading_ones != all_ones:
[463] Fix | Delete
byteslen = cls._max_prefixlen // 8
[464] Fix | Delete
details = ip_int.to_bytes(byteslen, 'big')
[465] Fix | Delete
msg = 'Netmask pattern %r mixes zeroes & ones'
[466] Fix | Delete
raise ValueError(msg % details)
[467] Fix | Delete
return prefixlen
[468] Fix | Delete
[469] Fix | Delete
@classmethod
[470] Fix | Delete
def _report_invalid_netmask(cls, netmask_str):
[471] Fix | Delete
msg = '%r is not a valid netmask' % netmask_str
[472] Fix | Delete
raise NetmaskValueError(msg) from None
[473] Fix | Delete
[474] Fix | Delete
@classmethod
[475] Fix | Delete
def _prefix_from_prefix_string(cls, prefixlen_str):
[476] Fix | Delete
"""Return prefix length from a numeric string
[477] Fix | Delete
[478] Fix | Delete
Args:
[479] Fix | Delete
prefixlen_str: The string to be converted
[480] Fix | Delete
[481] Fix | Delete
Returns:
[482] Fix | Delete
An integer, the prefix length.
[483] Fix | Delete
[484] Fix | Delete
Raises:
[485] Fix | Delete
NetmaskValueError: If the input is not a valid netmask
[486] Fix | Delete
"""
[487] Fix | Delete
# int allows a leading +/- as well as surrounding whitespace,
[488] Fix | Delete
# so we ensure that isn't the case
[489] Fix | Delete
if not (prefixlen_str.isascii() and prefixlen_str.isdigit()):
[490] Fix | Delete
cls._report_invalid_netmask(prefixlen_str)
[491] Fix | Delete
try:
[492] Fix | Delete
prefixlen = int(prefixlen_str)
[493] Fix | Delete
except ValueError:
[494] Fix | Delete
cls._report_invalid_netmask(prefixlen_str)
[495] Fix | Delete
if not (0 <= prefixlen <= cls._max_prefixlen):
[496] Fix | Delete
cls._report_invalid_netmask(prefixlen_str)
[497] Fix | Delete
return prefixlen
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function