输入文件下方有:
ipnetwork:111.0.1.0/255.255.255.0
我正在使用python字典读取此输入文件,如下所示。
d = {}
with open('inputfile.txt') as f:
for line in f:
if ":" not in line:
continue
key, value = line.strip().split(":", 1)
d[key] = value
for key, value in d.iteritems():
if key == 'ipnetwork':
ip = value
现在我需要帮助来验证提供的输入。
ipnetwork必须低于以下格式:
1.2.2.0/255.255.0.0--valid input
111.111.111.0/255.0.0.0 --valid input
256.500.111.0/0.0.0.0 --not valid input since ip does not contain more than 255 number
10.10.aa.1/255.255.255.0 --not valid input since ip address does not contain alphabets
答案 0 :(得分:0)
我使用netaddr模块获得了解决方案。只想在这里发布测试结果。
I got below netaddr tool it will validate above. below is the tested results
>>> from netaddr import *
>>> ip = IPNetwork('1.1.1.0')
>>> ip = IPNetwork('1.1.1.0/255.255.255.0')
>>> ip = IPNetwork('1.1.1.0/255.255.256.0')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\netaddr\ip\__init__.py", line 938, in
__init__
raise AddrFormatError('invalid IPNetwork %s' % addr)
netaddr.core.AddrFormatError: invalid IPNetwork 1.1.1.0/255.255.256.0
>>> ip = IPNetwork('1.a.1.0/255.255.255.0')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\netaddr\ip\__init__.py", line 938, in
__init__
raise AddrFormatError('invalid IPNetwork %s' % addr)
netaddr.core.AddrFormatError: invalid IPNetwork 1.a.1.0/255.255.255.0