我想从2个给定的IP地址计算IP地址的数量。
例如:127.0.1.10和127.0.0.200是67个IP地址。
这样做的简单方法是什么?
我看过其他示例,但是我正在寻找一个Python示例。谢谢。
>>> import socket
>>> def iptoint(ip):
... return int(socket.inet_aton(ip).encode('hex'), 16)
...
>>> iptoint('127.0.0.200')
2130706632
>>> iptoint('127.0.1.10') - iptoint('127.0.0.200')
66
>>> def ipdistance(ip1, ip2):
... return abs(iptoint(ip1) - iptoint(ip2)) + 1
...
>>> ipdistance('127.0.1.10', '127.0.0.200')
67
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句