校驗Ipv4 ipv6的格式 python
阿新 • • 發佈:2018-11-05
校驗Ipv4 ipv6的格式 python
1、校驗Ipv4的格式
#校驗Ipv4的格式
def checkipv4(ipv4):
matchobj = re.match(r'^(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))$',ipv4)
if matchobj:
return True
else:
return False
測試用例:
class IPv4MethodsTest (unittest.TestCase):
def test_checkipv4(self):
self.assertEqual(IPNumConvertMethods.checkipv4(''), False)
self.assertEqual(IPNumConvertMethods.checkipv4('256.1.1.1'), False)
self.assertEqual(IPNumConvertMethods.checkipv4('255.255.255.255'), True)
self.assertEqual( IPNumConvertMethods.checkipv4('0.0.0.0'), True)
self.assertEqual(IPNumConvertMethods.checkipv4('192.168.2.2'), True)
self.assertEqual(IPNumConvertMethods.checkipv4('-192.168.2.2'), False)
self.assertEqual(IPNumConvertMethods.checkipv4('255.255.255.256'), False)
2、校驗Ipv6的格式
IPv6的地址長度為128位,是IPv4地址長度的4倍。於是IPv4點分十進位制格式不再適用,採用十六進位制表示。IPv6有3種表示方法。
2.1 冒分十六進位制表示法
格式為X:X:X:X:X:X:X:X,其中每個X表示地址中的16位,以十六進位制表示,例如:
ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
這種表示法中,每個X的前導0是可以省略的,例如:
2001:0DB8:0000:0023:0008:0800:200C:417A→ 2001:DB8:0:23:8:800:200C:417A
2.2 0位壓縮表示法
在某些情況下,一個IPv6地址中問可能包含很長的一段0,可以把連續的一段0壓縮為“::”。但為保證地址解析的唯一性,地址中”::”只能出現一次,例如:
FF01:0:0:0:0:0:0:1101 → FF01::1101
0:0:0:0:0:0:0:1 → ::1
0:0:0:0:0:0:0:0 → ::
2.3 內嵌IPv4地址表示法
為了實現IPv4-IPv6互通,IPv4地址會嵌入IPv6地址中,此時地址常表示為:X:X:X:X:X:X:d.d.d.d,前96b採用冒分十六進位制表示,而最後32b地址則使用IPv4的點分十進位制表示,例如::192.168.0.1與::FFFF:192.168.0.1就是兩個典型的例子,注意在前96b中,壓縮0位的方法依舊適用。
def checkipv6(ipv6):
matchobj = re.match(r'^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$',ipv6)
if matchobj:
return True
else:
return False
測試用例:
class IPv4MethodsTest(unittest.TestCase):
def test_checkipv6(self):
self.assertEqual(IPNumConvertMethods.checkipv6(''),False)
self.assertEqual(IPNumConvertMethods.checkipv6('5e::5668::eeee'), False)
self.assertEqual(IPNumConvertMethods.checkipv6('55555:5e:0:0:0:0:0:5668:eeee'), False)
self.assertEqual(IPNumConvertMethods.checkipv6('55555:5e:0:0:0:0:0'), False)
self.assertEqual(IPNumConvertMethods.checkipv6('2001:DB8:0:23:8:800:200C:417A'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('FF01::1101'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('::1'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('5555:5e::'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('::'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('FFFF::192.168.0.1'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('::192.168.0.1'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('::FFFF:192.168.0.1'), True)
self.assertEqual(IPNumConvertMethods.checkipv6('2001:DB8:0:23:8:800:192.168.0.1'), True)
還有其他的一些,沒寫到腳本里
{"1:12:123:1234:2345:3456:4567:abCD", "true"},
{"1:12:123:1234:2345:3456:4567::", "true"},
{"1:12:123:1234:2345:3456::", "true"},
{"1:12:123:1234:2345::", "true"},
{"1:12:123:1234::", "true"},
{"1:12:123::", "true"},
{"1:12::", "true"},
{"1::", "true"},
{"::abCD", "true"},
{"::4567:abCD", "true"},
{"::3456:4567:abCD", "true"},
{"::2345:3456:4567:abCD", "true"},
{"::1234:2345:3456:4567:abCD", "true"},
{"::123:1234:2345:3456:4567:abCD", "true"},
{"::12:123:1234:2345:3456:4567:abCD", "true"},
{"1::123:1234:2345:3456:4567:abCD", "true"},
{"1:12::1234:2345:3456:4567:abCD", "true"},
{"1:12:123::2345:3456:4567:abCD", "true"},
{"1:12:123:1234::3456:4567:abCD", "true"},
{"1:12:123:1234:2345::4567:abCD", "true"},
{"1:12:123:1234:2345:3456::abCD", "true"},
{"1::1234:2345:3456:4567:abCD", "true"},
{"1:12::2345:3456:4567:abCD", "true"},
{"1:12:123::3456:4567:abCD", "true"},
{"1:12:123:1234::4567:abCD", "true"},
{"1:12:123:1234:2345::abCD", "true"},
{"1::2345:3456:4567:abCD", "true"},
{"1:12::3456:4567:abCD", "true"},
{"1:12:123::4567:abCD", "true"},
{"1:12:123:1234::abCD", "true"},
{"1::3456:4567:abCD", "true"},
{"1:12::4567:abCD", "true"},
{"1:12:123::abCD", "true"},
{"1::4567:abCD", "true"},
{"1:12::abCD", "true"},
{"1::abCD", "true"},
{"::", "true"},
{"1:12:123:1234:2345:3456:192.168.0.1", "true"},
{"1:12:123:1234:2345:3456:192.168.0.1", "true"},
{"::12:123:1234:2345:3456:192.168.0.1", "true"},
{"1::123:1234:2345:3456:192.168.0.1", "true"},
{"1:12::1234:2345:3456:192.168.0.1", "true"},
{"1:12:123::2345:3456:192.168.0.1", "true"},
{"1:12:123:1234::3456:192.168.0.1", "true"},
{"1:12:123:1234:2345::192.168.0.1", "true"},
{"::123:1234:2345:3456:192.168.0.1", "true"},
{"1::1234:2345:3456:192.168.0.1", "true"},
{"1:12::2345:3456:192.168.0.1", "true"},
{"1:12:123::3456:192.168.0.1", "true"},
{"1:12:123:1234::192.168.0.1", "true"},
{"::1234:2345:3456:192.168.0.1", "true"},
{"1::2345:3456:192.168.0.1", "true"},
{"1:12::3456:192.168.0.1", "true"},
{"1:12:123::192.168.0.1", "true"},
{"::2345:3456:192.168.0.1", "true"},
{"1::3456:192.168.0.1", "true"},
{"1:12::192.168.0.1", "true"},
{"::3456:192.168.0.1", "true"},
{"1::192.168.0.1", "true"},
{"::192.168.0.1", "true"},
{"1:12:123:1234:2345:3456:192.168.0.1", "true"},
{"::192.168.0.1", "true"},