Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib/python3..../site-pac...
File: IPy.py
"""
[0] Fix | Delete
IPy - class and tools for handling of IPv4 and IPv6 addresses and networks.
[1] Fix | Delete
See README file for learn how to use IPy.
[2] Fix | Delete
[3] Fix | Delete
Further Information might be available at:
[4] Fix | Delete
https://github.com/haypo/python-ipy
[5] Fix | Delete
"""
[6] Fix | Delete
[7] Fix | Delete
__version__ = '1.00'
[8] Fix | Delete
[9] Fix | Delete
import bisect
[10] Fix | Delete
import collections
[11] Fix | Delete
import sys
[12] Fix | Delete
import types
[13] Fix | Delete
[14] Fix | Delete
# Definition of the Ranges for IPv4 IPs
[15] Fix | Delete
# this should include www.iana.org/assignments/ipv4-address-space
[16] Fix | Delete
# and www.iana.org/assignments/multicast-addresses
[17] Fix | Delete
IPv4ranges = {
[18] Fix | Delete
'0': 'PUBLIC', # fall back
[19] Fix | Delete
'00000000': 'PRIVATE', # 0/8
[20] Fix | Delete
'00001010': 'PRIVATE', # 10/8
[21] Fix | Delete
'0110010001': 'CARRIER_GRADE_NAT', #100.64/10
[22] Fix | Delete
'01111111': 'LOOPBACK', # 127.0/8
[23] Fix | Delete
'1': 'PUBLIC', # fall back
[24] Fix | Delete
'1010100111111110': 'PRIVATE', # 169.254/16
[25] Fix | Delete
'101011000001': 'PRIVATE', # 172.16/12
[26] Fix | Delete
'1100000010101000': 'PRIVATE', # 192.168/16
[27] Fix | Delete
'111': 'RESERVED', # 224/3
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
# Definition of the Ranges for IPv6 IPs
[31] Fix | Delete
# http://www.iana.org/assignments/ipv6-address-space/
[32] Fix | Delete
# http://www.iana.org/assignments/ipv6-unicast-address-assignments/
[33] Fix | Delete
# http://www.iana.org/assignments/ipv6-multicast-addresses/
[34] Fix | Delete
IPv6ranges = {
[35] Fix | Delete
'00000000' : 'RESERVED', # ::/8
[36] Fix | Delete
'0' * 96 : 'RESERVED', # ::/96 Formerly IPV4COMP [RFC4291]
[37] Fix | Delete
'0' * 128 : 'UNSPECIFIED', # ::/128
[38] Fix | Delete
'0' * 127 + '1' : 'LOOPBACK', # ::1/128
[39] Fix | Delete
'0' * 80 + '1' * 16 : 'IPV4MAP', # ::ffff:0:0/96
[40] Fix | Delete
'00000000011001001111111110011011' + '0' * 64 : 'WKP46TRANS', # 0064:ff9b::/96 Well-Known-Prefix [RFC6052]
[41] Fix | Delete
'00000001' : 'UNASSIGNED', # 0100::/8
[42] Fix | Delete
'0000001' : 'RESERVED', # 0200::/7 Formerly NSAP [RFC4048]
[43] Fix | Delete
'0000010' : 'RESERVED', # 0400::/7 Formerly IPX [RFC3513]
[44] Fix | Delete
'0000011' : 'RESERVED', # 0600::/7
[45] Fix | Delete
'00001' : 'RESERVED', # 0800::/5
[46] Fix | Delete
'0001' : 'RESERVED', # 1000::/4
[47] Fix | Delete
'001' : 'GLOBAL-UNICAST', # 2000::/3 [RFC4291]
[48] Fix | Delete
'00100000000000010000000' : 'SPECIALPURPOSE', # 2001::/23 [RFC4773]
[49] Fix | Delete
'00100000000000010000000000000000' : 'TEREDO', # 2001::/32 [RFC4380]
[50] Fix | Delete
'00100000000000010000000000000010' + '0' * 16 : 'BMWG', # 2001:0002::/48 Benchmarking [RFC5180]
[51] Fix | Delete
'0010000000000001000000000001' : 'ORCHID', # 2001:0010::/28 (Temp until 2014-03-21) [RFC4843]
[52] Fix | Delete
'00100000000000010000001' : 'ALLOCATED APNIC', # 2001:0200::/23
[53] Fix | Delete
'00100000000000010000010' : 'ALLOCATED ARIN', # 2001:0400::/23
[54] Fix | Delete
'00100000000000010000011' : 'ALLOCATED RIPE NCC', # 2001:0600::/23
[55] Fix | Delete
'00100000000000010000100' : 'ALLOCATED RIPE NCC', # 2001:0800::/23
[56] Fix | Delete
'00100000000000010000101' : 'ALLOCATED RIPE NCC', # 2001:0a00::/23
[57] Fix | Delete
'00100000000000010000110' : 'ALLOCATED APNIC', # 2001:0c00::/23
[58] Fix | Delete
'00100000000000010000110110111000' : 'DOCUMENTATION', # 2001:0db8::/32 [RFC3849]
[59] Fix | Delete
'00100000000000010000111' : 'ALLOCATED APNIC', # 2001:0e00::/23
[60] Fix | Delete
'00100000000000010001001' : 'ALLOCATED LACNIC', # 2001:1200::/23
[61] Fix | Delete
'00100000000000010001010' : 'ALLOCATED RIPE NCC', # 2001:1400::/23
[62] Fix | Delete
'00100000000000010001011' : 'ALLOCATED RIPE NCC', # 2001:1600::/23
[63] Fix | Delete
'00100000000000010001100' : 'ALLOCATED ARIN', # 2001:1800::/23
[64] Fix | Delete
'00100000000000010001101' : 'ALLOCATED RIPE NCC', # 2001:1a00::/23
[65] Fix | Delete
'0010000000000001000111' : 'ALLOCATED RIPE NCC', # 2001:1c00::/22
[66] Fix | Delete
'00100000000000010010' : 'ALLOCATED RIPE NCC', # 2001:2000::/20
[67] Fix | Delete
'001000000000000100110' : 'ALLOCATED RIPE NCC', # 2001:3000::/21
[68] Fix | Delete
'0010000000000001001110' : 'ALLOCATED RIPE NCC', # 2001:3800::/22
[69] Fix | Delete
'0010000000000001001111' : 'RESERVED', # 2001:3c00::/22 Possible future allocation to RIPE NCC
[70] Fix | Delete
'00100000000000010100000' : 'ALLOCATED RIPE NCC', # 2001:4000::/23
[71] Fix | Delete
'00100000000000010100001' : 'ALLOCATED AFRINIC', # 2001:4200::/23
[72] Fix | Delete
'00100000000000010100010' : 'ALLOCATED APNIC', # 2001:4400::/23
[73] Fix | Delete
'00100000000000010100011' : 'ALLOCATED RIPE NCC', # 2001:4600::/23
[74] Fix | Delete
'00100000000000010100100' : 'ALLOCATED ARIN', # 2001:4800::/23
[75] Fix | Delete
'00100000000000010100101' : 'ALLOCATED RIPE NCC', # 2001:4a00::/23
[76] Fix | Delete
'00100000000000010100110' : 'ALLOCATED RIPE NCC', # 2001:4c00::/23
[77] Fix | Delete
'00100000000000010101' : 'ALLOCATED RIPE NCC', # 2001:5000::/20
[78] Fix | Delete
'0010000000000001100' : 'ALLOCATED APNIC', # 2001:8000::/19
[79] Fix | Delete
'00100000000000011010' : 'ALLOCATED APNIC', # 2001:a000::/20
[80] Fix | Delete
'00100000000000011011' : 'ALLOCATED APNIC', # 2001:b000::/20
[81] Fix | Delete
'0010000000000010' : '6TO4', # 2002::/16 "6to4" [RFC3056]
[82] Fix | Delete
'001000000000001100' : 'ALLOCATED RIPE NCC', # 2003::/18
[83] Fix | Delete
'001001000000' : 'ALLOCATED APNIC', # 2400::/12
[84] Fix | Delete
'001001100000' : 'ALLOCATED ARIN', # 2600::/12
[85] Fix | Delete
'00100110000100000000000' : 'ALLOCATED ARIN', # 2610::/23
[86] Fix | Delete
'00100110001000000000000' : 'ALLOCATED ARIN', # 2620::/23
[87] Fix | Delete
'001010000000' : 'ALLOCATED LACNIC', # 2800::/12
[88] Fix | Delete
'001010100000' : 'ALLOCATED RIPE NCC', # 2a00::/12
[89] Fix | Delete
'001011000000' : 'ALLOCATED AFRINIC', # 2c00::/12
[90] Fix | Delete
'00101101' : 'RESERVED', # 2d00::/8
[91] Fix | Delete
'0010111' : 'RESERVED', # 2e00::/7
[92] Fix | Delete
'0011' : 'RESERVED', # 3000::/4
[93] Fix | Delete
'010' : 'RESERVED', # 4000::/3
[94] Fix | Delete
'011' : 'RESERVED', # 6000::/3
[95] Fix | Delete
'100' : 'RESERVED', # 8000::/3
[96] Fix | Delete
'101' : 'RESERVED', # a000::/3
[97] Fix | Delete
'110' : 'RESERVED', # c000::/3
[98] Fix | Delete
'1110' : 'RESERVED', # e000::/4
[99] Fix | Delete
'11110' : 'RESERVED', # f000::/5
[100] Fix | Delete
'111110' : 'RESERVED', # f800::/6
[101] Fix | Delete
'1111110' : 'ULA', # fc00::/7 [RFC4193]
[102] Fix | Delete
'111111100' : 'RESERVED', # fe00::/9
[103] Fix | Delete
'1111111010' : 'LINKLOCAL', # fe80::/10
[104] Fix | Delete
'1111111011' : 'RESERVED', # fec0::/10 Formerly SITELOCAL [RFC4291]
[105] Fix | Delete
'11111111' : 'MULTICAST', # ff00::/8
[106] Fix | Delete
'1111111100000001' : 'NODE-LOCAL MULTICAST', # ff01::/16
[107] Fix | Delete
'1111111100000010' : 'LINK-LOCAL MULTICAST', # ff02::/16
[108] Fix | Delete
'1111111100000100' : 'ADMIN-LOCAL MULTICAST', # ff04::/16
[109] Fix | Delete
'1111111100000101' : 'SITE-LOCAL MULTICAST', # ff05::/16
[110] Fix | Delete
'1111111100001000' : 'ORG-LOCAL MULTICAST', # ff08::/16
[111] Fix | Delete
'1111111100001110' : 'GLOBAL MULTICAST', # ff0e::/16
[112] Fix | Delete
'1111111100001111' : 'RESERVED MULTICAST', # ff0f::/16
[113] Fix | Delete
'111111110011' : 'PREFIX-BASED MULTICAST', # ff30::/12 [RFC3306]
[114] Fix | Delete
'111111110111' : 'RP-EMBEDDED MULTICAST', # ff70::/12 [RFC3956]
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
MAX_IPV4_ADDRESS = 0xffffffff
[118] Fix | Delete
MAX_IPV6_ADDRESS = 0xffffffffffffffffffffffffffffffff
[119] Fix | Delete
IPV6_TEST_MAP = 0xffffffffffffffffffffffff00000000
[120] Fix | Delete
IPV6_MAP_MASK = 0x00000000000000000000ffff00000000
[121] Fix | Delete
[122] Fix | Delete
if sys.version_info >= (3,):
[123] Fix | Delete
INT_TYPES = (int,)
[124] Fix | Delete
STR_TYPES = (str,)
[125] Fix | Delete
xrange = range
[126] Fix | Delete
else:
[127] Fix | Delete
INT_TYPES = (int, long)
[128] Fix | Delete
STR_TYPES = (str, unicode)
[129] Fix | Delete
[130] Fix | Delete
[131] Fix | Delete
class IPint(object):
[132] Fix | Delete
"""Handling of IP addresses returning integers.
[133] Fix | Delete
[134] Fix | Delete
Use class IP instead because some features are not implemented for
[135] Fix | Delete
IPint."""
[136] Fix | Delete
[137] Fix | Delete
def __init__(self, data, ipversion=0, make_net=0):
[138] Fix | Delete
"""Create an instance of an IP object.
[139] Fix | Delete
[140] Fix | Delete
Data can be a network specification or a single IP. IP
[141] Fix | Delete
addresses can be specified in all forms understood by
[142] Fix | Delete
parseAddress(). The size of a network can be specified as
[143] Fix | Delete
[144] Fix | Delete
/prefixlen a.b.c.0/24 2001:658:22a:cafe::/64
[145] Fix | Delete
-lastIP a.b.c.0-a.b.c.255 2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff
[146] Fix | Delete
/decimal netmask a.b.c.d/255.255.255.0 not supported for IPv6
[147] Fix | Delete
[148] Fix | Delete
If no size specification is given a size of 1 address (/32 for
[149] Fix | Delete
IPv4 and /128 for IPv6) is assumed.
[150] Fix | Delete
[151] Fix | Delete
If make_net is True, an IP address will be transformed into the network
[152] Fix | Delete
address by applying the specified netmask.
[153] Fix | Delete
[154] Fix | Delete
>>> print(IP('127.0.0.0/8'))
[155] Fix | Delete
127.0.0.0/8
[156] Fix | Delete
>>> print(IP('127.0.0.0/255.0.0.0'))
[157] Fix | Delete
127.0.0.0/8
[158] Fix | Delete
>>> print(IP('127.0.0.0-127.255.255.255'))
[159] Fix | Delete
127.0.0.0/8
[160] Fix | Delete
>>> print(IP('127.0.0.1/255.0.0.0', make_net=True))
[161] Fix | Delete
127.0.0.0/8
[162] Fix | Delete
[163] Fix | Delete
See module documentation for more examples.
[164] Fix | Delete
"""
[165] Fix | Delete
[166] Fix | Delete
# Print no Prefixlen for /32 and /128
[167] Fix | Delete
self.NoPrefixForSingleIp = 1
[168] Fix | Delete
[169] Fix | Delete
# Do we want prefix printed by default? see _printPrefix()
[170] Fix | Delete
self.WantPrefixLen = None
[171] Fix | Delete
[172] Fix | Delete
netbits = 0
[173] Fix | Delete
prefixlen = -1
[174] Fix | Delete
[175] Fix | Delete
# handling of non string values in constructor
[176] Fix | Delete
if isinstance(data, INT_TYPES):
[177] Fix | Delete
self.ip = int(data)
[178] Fix | Delete
if ipversion == 0:
[179] Fix | Delete
if self.ip <= MAX_IPV4_ADDRESS:
[180] Fix | Delete
ipversion = 4
[181] Fix | Delete
else:
[182] Fix | Delete
ipversion = 6
[183] Fix | Delete
if ipversion == 4:
[184] Fix | Delete
if self.ip > MAX_IPV4_ADDRESS:
[185] Fix | Delete
raise ValueError("IPv4 Address can't be larger than %x: %x" % (MAX_IPV4_ADDRESS, self.ip))
[186] Fix | Delete
prefixlen = 32
[187] Fix | Delete
elif ipversion == 6:
[188] Fix | Delete
if self.ip > MAX_IPV6_ADDRESS:
[189] Fix | Delete
raise ValueError("IPv6 Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, self.ip))
[190] Fix | Delete
prefixlen = 128
[191] Fix | Delete
else:
[192] Fix | Delete
raise ValueError("only IPv4 and IPv6 supported")
[193] Fix | Delete
self._ipversion = ipversion
[194] Fix | Delete
self._prefixlen = prefixlen
[195] Fix | Delete
# handle IP instance as an parameter
[196] Fix | Delete
elif isinstance(data, IPint):
[197] Fix | Delete
self._ipversion = data._ipversion
[198] Fix | Delete
self._prefixlen = data._prefixlen
[199] Fix | Delete
self.ip = data.ip
[200] Fix | Delete
elif isinstance(data, STR_TYPES):
[201] Fix | Delete
# TODO: refactor me!
[202] Fix | Delete
# splitting of a string into IP and prefixlen et. al.
[203] Fix | Delete
x = data.split('-')
[204] Fix | Delete
if len(x) == 2:
[205] Fix | Delete
# a.b.c.0-a.b.c.255 specification ?
[206] Fix | Delete
(ip, last) = x
[207] Fix | Delete
(self.ip, parsedVersion) = parseAddress(ip)
[208] Fix | Delete
if parsedVersion != 4:
[209] Fix | Delete
raise ValueError("first-last notation only allowed for IPv4")
[210] Fix | Delete
(last, lastversion) = parseAddress(last)
[211] Fix | Delete
if lastversion != 4:
[212] Fix | Delete
raise ValueError("last address should be IPv4, too")
[213] Fix | Delete
if last < self.ip:
[214] Fix | Delete
raise ValueError("last address should be larger than first")
[215] Fix | Delete
size = last - self.ip
[216] Fix | Delete
netbits = _count1Bits(size)
[217] Fix | Delete
# make sure the broadcast is the same as the last ip
[218] Fix | Delete
# otherwise it will return /16 for something like:
[219] Fix | Delete
# 192.168.0.0-192.168.191.255
[220] Fix | Delete
if IP('%s/%s' % (ip, 32-netbits)).broadcast().int() != last:
[221] Fix | Delete
raise ValueError("the range %s is not on a network boundary." % data)
[222] Fix | Delete
elif len(x) == 1:
[223] Fix | Delete
x = data.split('/')
[224] Fix | Delete
# if no prefix is given use defaults
[225] Fix | Delete
if len(x) == 1:
[226] Fix | Delete
ip = x[0]
[227] Fix | Delete
prefixlen = -1
[228] Fix | Delete
elif len(x) > 2:
[229] Fix | Delete
raise ValueError("only one '/' allowed in IP Address")
[230] Fix | Delete
else:
[231] Fix | Delete
(ip, prefixlen) = x
[232] Fix | Delete
if prefixlen.find('.') != -1:
[233] Fix | Delete
# check if the user might have used a netmask like
[234] Fix | Delete
# a.b.c.d/255.255.255.0
[235] Fix | Delete
(netmask, vers) = parseAddress(prefixlen)
[236] Fix | Delete
if vers != 4:
[237] Fix | Delete
raise ValueError("netmask must be IPv4")
[238] Fix | Delete
prefixlen = _netmaskToPrefixlen(netmask)
[239] Fix | Delete
elif len(x) > 2:
[240] Fix | Delete
raise ValueError("only one '-' allowed in IP Address")
[241] Fix | Delete
else:
[242] Fix | Delete
raise ValueError("can't parse")
[243] Fix | Delete
[244] Fix | Delete
(self.ip, parsedVersion) = parseAddress(ip, ipversion)
[245] Fix | Delete
if ipversion == 0:
[246] Fix | Delete
ipversion = parsedVersion
[247] Fix | Delete
if prefixlen == -1:
[248] Fix | Delete
bits = _ipVersionToLen(ipversion)
[249] Fix | Delete
prefixlen = bits - netbits
[250] Fix | Delete
self._ipversion = ipversion
[251] Fix | Delete
self._prefixlen = int(prefixlen)
[252] Fix | Delete
[253] Fix | Delete
if make_net:
[254] Fix | Delete
self.ip = self.ip & _prefixlenToNetmask(self._prefixlen, self._ipversion)
[255] Fix | Delete
[256] Fix | Delete
if not _checkNetaddrWorksWithPrefixlen(self.ip,
[257] Fix | Delete
self._prefixlen, self._ipversion):
[258] Fix | Delete
raise ValueError("%s has invalid prefix length (%s)" % (repr(self), self._prefixlen))
[259] Fix | Delete
else:
[260] Fix | Delete
raise TypeError("Unsupported data type: %s" % type(data))
[261] Fix | Delete
[262] Fix | Delete
def int(self):
[263] Fix | Delete
"""Return the first / base / network addess as an (long) integer.
[264] Fix | Delete
[265] Fix | Delete
The same as IP[0].
[266] Fix | Delete
[267] Fix | Delete
>>> "%X" % IP('10.0.0.0/8').int()
[268] Fix | Delete
'A000000'
[269] Fix | Delete
"""
[270] Fix | Delete
return self.ip
[271] Fix | Delete
[272] Fix | Delete
def version(self):
[273] Fix | Delete
"""Return the IP version of this Object.
[274] Fix | Delete
[275] Fix | Delete
>>> IP('10.0.0.0/8').version()
[276] Fix | Delete
4
[277] Fix | Delete
>>> IP('::1').version()
[278] Fix | Delete
6
[279] Fix | Delete
"""
[280] Fix | Delete
return self._ipversion
[281] Fix | Delete
[282] Fix | Delete
def prefixlen(self):
[283] Fix | Delete
"""Returns Network Prefixlen.
[284] Fix | Delete
[285] Fix | Delete
>>> IP('10.0.0.0/8').prefixlen()
[286] Fix | Delete
8
[287] Fix | Delete
"""
[288] Fix | Delete
return self._prefixlen
[289] Fix | Delete
[290] Fix | Delete
def net(self):
[291] Fix | Delete
"""
[292] Fix | Delete
Return the base (first) address of a network as an (long) integer.
[293] Fix | Delete
"""
[294] Fix | Delete
return self.int()
[295] Fix | Delete
[296] Fix | Delete
def broadcast(self):
[297] Fix | Delete
"""
[298] Fix | Delete
Return the broadcast (last) address of a network as an (long) integer.
[299] Fix | Delete
[300] Fix | Delete
The same as IP[-1]."""
[301] Fix | Delete
return self.int() + self.len() - 1
[302] Fix | Delete
[303] Fix | Delete
def _printPrefix(self, want):
[304] Fix | Delete
"""Prints Prefixlen/Netmask.
[305] Fix | Delete
[306] Fix | Delete
Not really. In fact it is our universal Netmask/Prefixlen printer.
[307] Fix | Delete
This is considered an internal function.
[308] Fix | Delete
[309] Fix | Delete
want == 0 / None don't return anything 1.2.3.0
[310] Fix | Delete
want == 1 /prefix 1.2.3.0/24
[311] Fix | Delete
want == 2 /netmask 1.2.3.0/255.255.255.0
[312] Fix | Delete
want == 3 -lastip 1.2.3.0-1.2.3.255
[313] Fix | Delete
"""
[314] Fix | Delete
[315] Fix | Delete
if (self._ipversion == 4 and self._prefixlen == 32) or \
[316] Fix | Delete
(self._ipversion == 6 and self._prefixlen == 128):
[317] Fix | Delete
if self.NoPrefixForSingleIp:
[318] Fix | Delete
want = 0
[319] Fix | Delete
if want == None:
[320] Fix | Delete
want = self.WantPrefixLen
[321] Fix | Delete
if want == None:
[322] Fix | Delete
want = 1
[323] Fix | Delete
if want:
[324] Fix | Delete
if want == 2:
[325] Fix | Delete
# this should work with IP and IPint
[326] Fix | Delete
netmask = self.netmask()
[327] Fix | Delete
if not isinstance(netmask, INT_TYPES):
[328] Fix | Delete
netmask = netmask.int()
[329] Fix | Delete
return "/%s" % (intToIp(netmask, self._ipversion))
[330] Fix | Delete
elif want == 3:
[331] Fix | Delete
return "-%s" % (intToIp(self.ip + self.len() - 1, self._ipversion))
[332] Fix | Delete
else:
[333] Fix | Delete
# default
[334] Fix | Delete
return "/%d" % (self._prefixlen)
[335] Fix | Delete
else:
[336] Fix | Delete
return ''
[337] Fix | Delete
[338] Fix | Delete
# We have different flavours to convert to:
[339] Fix | Delete
# strFullsize 127.0.0.1 2001:0658:022a:cafe:0200:c0ff:fe8d:08fa
[340] Fix | Delete
# strNormal 127.0.0.1 2001:658:22a:cafe:200:c0ff:fe8d:08fa
[341] Fix | Delete
# strCompressed 127.0.0.1 2001:658:22a:cafe::1
[342] Fix | Delete
# strHex 0x7F000001 0x20010658022ACAFE0200C0FFFE8D08FA
[343] Fix | Delete
# strDec 2130706433 42540616829182469433547974687817795834
[344] Fix | Delete
[345] Fix | Delete
def strBin(self, wantprefixlen = None):
[346] Fix | Delete
"""Return a string representation as a binary value.
[347] Fix | Delete
[348] Fix | Delete
>>> print(IP('127.0.0.1').strBin())
[349] Fix | Delete
01111111000000000000000000000001
[350] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').strBin())
[351] Fix | Delete
00100000000000010000011001011000000000100010101011001010111111100000001000000000000000000000000000000000000000000000000000000001
[352] Fix | Delete
"""
[353] Fix | Delete
[354] Fix | Delete
bits = _ipVersionToLen(self._ipversion)
[355] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[356] Fix | Delete
wantprefixlen = 0
[357] Fix | Delete
ret = _intToBin(self.ip)
[358] Fix | Delete
return '0' * (bits - len(ret)) + ret + self._printPrefix(wantprefixlen)
[359] Fix | Delete
[360] Fix | Delete
def strCompressed(self, wantprefixlen = None):
[361] Fix | Delete
"""Return a string representation in compressed format using '::' Notation.
[362] Fix | Delete
[363] Fix | Delete
>>> IP('127.0.0.1').strCompressed()
[364] Fix | Delete
'127.0.0.1'
[365] Fix | Delete
>>> IP('2001:0658:022a:cafe:0200::1').strCompressed()
[366] Fix | Delete
'2001:658:22a:cafe:200::1'
[367] Fix | Delete
>>> IP('ffff:ffff:ffff:ffff:ffff:f:f:fffc/127').strCompressed()
[368] Fix | Delete
'ffff:ffff:ffff:ffff:ffff:f:f:fffc/127'
[369] Fix | Delete
"""
[370] Fix | Delete
[371] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[372] Fix | Delete
wantprefixlen = 1
[373] Fix | Delete
[374] Fix | Delete
if self._ipversion == 4:
[375] Fix | Delete
return self.strFullsize(wantprefixlen)
[376] Fix | Delete
else:
[377] Fix | Delete
if self.ip >> 32 == 0xffff:
[378] Fix | Delete
ipv4 = intToIp(self.ip & MAX_IPV4_ADDRESS, 4)
[379] Fix | Delete
text = "::ffff:" + ipv4 + self._printPrefix(wantprefixlen)
[380] Fix | Delete
return text
[381] Fix | Delete
# find the longest sequence of '0'
[382] Fix | Delete
hextets = [int(x, 16) for x in self.strFullsize(0).split(':')]
[383] Fix | Delete
# every element of followingzeros will contain the number of zeros
[384] Fix | Delete
# following the corresponding element of hextets
[385] Fix | Delete
followingzeros = [0] * 8
[386] Fix | Delete
for i in xrange(len(hextets)):
[387] Fix | Delete
followingzeros[i] = _countFollowingZeros(hextets[i:])
[388] Fix | Delete
# compressionpos is the position where we can start removing zeros
[389] Fix | Delete
compressionpos = followingzeros.index(max(followingzeros))
[390] Fix | Delete
if max(followingzeros) > 1:
[391] Fix | Delete
# genererate string with the longest number of zeros cut out
[392] Fix | Delete
# now we need hextets as strings
[393] Fix | Delete
hextets = [x for x in self.strNormal(0).split(':')]
[394] Fix | Delete
while compressionpos < len(hextets) and hextets[compressionpos] == '0':
[395] Fix | Delete
del(hextets[compressionpos])
[396] Fix | Delete
hextets.insert(compressionpos, '')
[397] Fix | Delete
if compressionpos + 1 >= len(hextets):
[398] Fix | Delete
hextets.append('')
[399] Fix | Delete
if compressionpos == 0:
[400] Fix | Delete
hextets = [''] + hextets
[401] Fix | Delete
return ':'.join(hextets) + self._printPrefix(wantprefixlen)
[402] Fix | Delete
else:
[403] Fix | Delete
return self.strNormal(0) + self._printPrefix(wantprefixlen)
[404] Fix | Delete
[405] Fix | Delete
def strNormal(self, wantprefixlen = None):
[406] Fix | Delete
"""Return a string representation in the usual format.
[407] Fix | Delete
[408] Fix | Delete
>>> print(IP('127.0.0.1').strNormal())
[409] Fix | Delete
127.0.0.1
[410] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').strNormal())
[411] Fix | Delete
2001:658:22a:cafe:200:0:0:1
[412] Fix | Delete
"""
[413] Fix | Delete
[414] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[415] Fix | Delete
wantprefixlen = 1
[416] Fix | Delete
[417] Fix | Delete
if self._ipversion == 4:
[418] Fix | Delete
ret = self.strFullsize(0)
[419] Fix | Delete
elif self._ipversion == 6:
[420] Fix | Delete
ret = ':'.join(["%x" % x for x in [int(x, 16) for x in self.strFullsize(0).split(':')]])
[421] Fix | Delete
else:
[422] Fix | Delete
raise ValueError("only IPv4 and IPv6 supported")
[423] Fix | Delete
[424] Fix | Delete
[425] Fix | Delete
[426] Fix | Delete
return ret + self._printPrefix(wantprefixlen)
[427] Fix | Delete
[428] Fix | Delete
def strFullsize(self, wantprefixlen = None):
[429] Fix | Delete
"""Return a string representation in the non-mangled format.
[430] Fix | Delete
[431] Fix | Delete
>>> print(IP('127.0.0.1').strFullsize())
[432] Fix | Delete
127.0.0.1
[433] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').strFullsize())
[434] Fix | Delete
2001:0658:022a:cafe:0200:0000:0000:0001
[435] Fix | Delete
"""
[436] Fix | Delete
[437] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[438] Fix | Delete
wantprefixlen = 1
[439] Fix | Delete
[440] Fix | Delete
return intToIp(self.ip, self._ipversion) + self._printPrefix(wantprefixlen)
[441] Fix | Delete
[442] Fix | Delete
def strHex(self, wantprefixlen = None):
[443] Fix | Delete
"""Return a string representation in hex format in lower case.
[444] Fix | Delete
[445] Fix | Delete
>>> print(IP('127.0.0.1').strHex())
[446] Fix | Delete
0x7f000001
[447] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').strHex())
[448] Fix | Delete
0x20010658022acafe0200000000000001
[449] Fix | Delete
"""
[450] Fix | Delete
[451] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[452] Fix | Delete
wantprefixlen = 0
[453] Fix | Delete
[454] Fix | Delete
x = '0x%x' % self.ip
[455] Fix | Delete
return x + self._printPrefix(wantprefixlen)
[456] Fix | Delete
[457] Fix | Delete
def strDec(self, wantprefixlen = None):
[458] Fix | Delete
"""Return a string representation in decimal format.
[459] Fix | Delete
[460] Fix | Delete
>>> print(IP('127.0.0.1').strDec())
[461] Fix | Delete
2130706433
[462] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').strDec())
[463] Fix | Delete
42540616829182469433547762482097946625
[464] Fix | Delete
"""
[465] Fix | Delete
[466] Fix | Delete
if self.WantPrefixLen == None and wantprefixlen == None:
[467] Fix | Delete
wantprefixlen = 0
[468] Fix | Delete
[469] Fix | Delete
x = '%d' % self.ip
[470] Fix | Delete
return x + self._printPrefix(wantprefixlen)
[471] Fix | Delete
[472] Fix | Delete
def iptype(self):
[473] Fix | Delete
"""Return a description of the IP type ('PRIVATE', 'RESERVED', etc).
[474] Fix | Delete
[475] Fix | Delete
>>> print(IP('127.0.0.1').iptype())
[476] Fix | Delete
LOOPBACK
[477] Fix | Delete
>>> print(IP('192.168.1.1').iptype())
[478] Fix | Delete
PRIVATE
[479] Fix | Delete
>>> print(IP('195.185.1.2').iptype())
[480] Fix | Delete
PUBLIC
[481] Fix | Delete
>>> print(IP('::1').iptype())
[482] Fix | Delete
LOOPBACK
[483] Fix | Delete
>>> print(IP('2001:0658:022a:cafe:0200::1').iptype())
[484] Fix | Delete
ALLOCATED RIPE NCC
[485] Fix | Delete
[486] Fix | Delete
The type information for IPv6 is out of sync with reality.
[487] Fix | Delete
"""
[488] Fix | Delete
[489] Fix | Delete
# this could be greatly improved
[490] Fix | Delete
[491] Fix | Delete
if self._ipversion == 4:
[492] Fix | Delete
iprange = IPv4ranges
[493] Fix | Delete
elif self._ipversion == 6:
[494] Fix | Delete
iprange = IPv6ranges
[495] Fix | Delete
else:
[496] Fix | Delete
raise ValueError("only IPv4 and IPv6 supported")
[497] Fix | Delete
[498] Fix | Delete
bits = self.strBin()
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function