使用python 中的IPy 验证IP格式是否合法

觉得python 很强大
可以用ipy判断ip地址的格式是否正确,如果不正确还可以指出错误原因,感觉学习计网的时候python很有用

>>> from IPy import IP
>>> IP('192.168.1.1').version()
4
>>> IP('fe08::5efe:172.16.18.3').version()
6
>>> IP('5401::13::b0c:3201').version()
Traceback (most recent call last):
  File "", line 1, in 
    IP('5401::13::b0c:3201').version()
  File "D:\python\lib\site-packages\IPy.py", line 246, in __init__
    (self.ip, parsedVersion) = parseAddress(ip)
  File "D:\python\lib\site-packages\IPy.py", line 1402, in parseAddress
    return (_parseAddressIPv6(ipstr), 6)
  File "D:\python\lib\site-packages\IPy.py", line 1281, in _parseAddressIPv6
    raise ValueError("%r: Invalid IPv6 address: more than one '::'" % ipstr)
ValueError: '5401::13::b0c:3201': Invalid IPv6 address: more than one '::'
>>> IP('3fae:3400:0001:0001:39d6:059c:41a6').version()
Traceback (most recent call last):
  File "", line 1, in 
    IP('3fae:3400:0001:0001:39d6:059c:41a6').version()
  File "D:\python\lib\site-packages\IPy.py", line 246, in __init__
    (self.ip, parsedVersion) = parseAddress(ip)
  File "D:\python\lib\site-packages\IPy.py", line 1402, in parseAddress
    return (_parseAddressIPv6(ipstr), 6)
  File "D:\python\lib\site-packages\IPy.py", line 1322, in _parseAddressIPv6
    raise ValueError("%r: Invalid IPv6 address: should have 8 hextets" % ipstr)
ValueError: '3fae:3400:0001:0001:39d6:059c:41a6': Invalid IPv6 address: should have 8 hextets

你可能感兴趣的:(使用python 中的IPy 验证IP格式是否合法)