将 IP 列表转换为相应 IP 范围的列表 (python)
Convert list of IPs to list of corresponding IP Ranges (python)
我想将 IP 列表转换为相应 IP 范围的列表。
例如:
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
到
ipranges = ['137.226.161.0/24', '134.130.4.0/24', '8.8.8.0/24', '8.8.4.0/24', '134.130.5.0/24']
最有效的方法是什么?我还没有找到提供类似功能的模块。这个功能的原因是一长串IP(超过1000个ips)应该转换成子网列表以提高可读性。
谢谢
如果我没理解错的话,你只想根据前 24 位 (/24) 相同进行匹配。对于这些任务,我推荐 set
:
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
ipset = set()
for i in iplist:
ipset.add(".".join(i.split(".")[:-1]))
ipranges = [p + ".0/24" for p in ipset]
print(ipranges)
这会打印:
['134.130.5.0/24', '8.8.4.0/24', '8.8.8.0/24', '134.130.4.0/24', '137.226.161.0/24']
那么这段代码有什么作用?
首先,我们遍历列表并截取每个IP的最后一段:
segments = "8.8.8.8".split(".") # segments == ["8", "8", "8", "8"]
segments_cut = segments[:-1] # segments_cut == ["8", "8", "8"]
prefix = ".".join(segments_cut) # prefix == "8.8.8"
现在我们将这些前缀添加到 set
。 Python set
只允许唯一元素。这导致:ìpset == {'134.130.5', '8.8.4', '8.8.8', '134.130.4', '137.226.161'}
最后我们遍历集合并附加后缀“.0/24”来表示子网。
编辑:关于 "efficiency"
我喜欢 ,但我知道我的解决方案明显更快(1.2 秒对 0.09 秒):
>>> import timeit
>>> # darkless' ipaddress solution
>>> timeit.timeit("[str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
1.186...
>>> # My solution
>>> timeit.timeit("[p + '.0/24' for p in {'.'.join(i.split('.')[:-1]) for i in iplist}]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
0.096...
正如 Hampus Larsson 提到的,您可以使用 python ipaddress 模块:
import ipaddress
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
ipranges = [str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]
>>> ipranges
['137.226.161.0/24', '134.130.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '8.8.8.0/24', '8.8.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '134.130.5.0/24']
我想将 IP 列表转换为相应 IP 范围的列表。 例如:
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
到
ipranges = ['137.226.161.0/24', '134.130.4.0/24', '8.8.8.0/24', '8.8.4.0/24', '134.130.5.0/24']
最有效的方法是什么?我还没有找到提供类似功能的模块。这个功能的原因是一长串IP(超过1000个ips)应该转换成子网列表以提高可读性。
谢谢
如果我没理解错的话,你只想根据前 24 位 (/24) 相同进行匹配。对于这些任务,我推荐 set
:
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
ipset = set()
for i in iplist:
ipset.add(".".join(i.split(".")[:-1]))
ipranges = [p + ".0/24" for p in ipset]
print(ipranges)
这会打印:
['134.130.5.0/24', '8.8.4.0/24', '8.8.8.0/24', '134.130.4.0/24', '137.226.161.0/24']
那么这段代码有什么作用?
首先,我们遍历列表并截取每个IP的最后一段:
segments = "8.8.8.8".split(".") # segments == ["8", "8", "8", "8"]
segments_cut = segments[:-1] # segments_cut == ["8", "8", "8"]
prefix = ".".join(segments_cut) # prefix == "8.8.8"
现在我们将这些前缀添加到 set
。 Python set
只允许唯一元素。这导致:ìpset == {'134.130.5', '8.8.4', '8.8.8', '134.130.4', '137.226.161'}
最后我们遍历集合并附加后缀“.0/24”来表示子网。
编辑:关于 "efficiency"
我喜欢
>>> import timeit
>>> # darkless' ipaddress solution
>>> timeit.timeit("[str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
1.186...
>>> # My solution
>>> timeit.timeit("[p + '.0/24' for p in {'.'.join(i.split('.')[:-1]) for i in iplist}]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
0.096...
正如 Hampus Larsson 提到的,您可以使用 python ipaddress 模块:
import ipaddress
iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']
ipranges = [str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]
>>> ipranges
['137.226.161.0/24', '134.130.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '8.8.8.0/24', '8.8.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '134.130.5.0/24']