在示例代码中,我将开始 IP 和结束 IP 作为输入。我将从中创建一个 IP 池列表。我想添加一个起始 IP 应始终小于结束 IP 的验证。
start_ip = '100.71.9.98'
end_ip = '100.71.9.100'
start_ip < end_ip
False
我如何验证 start_ip 应该小于 end_ip ?
最佳答案
将字符串转换为整数列表。
>>> start_ip = '100.71.9.98'
>>> end_ip = '100.71.9.100'
>>> map(int, start_ip.split('.')) # list(map(int, ...)) in Python 3.x
[100, 71, 9, 98]
>>> map(int, end_ip.split('.'))
[100, 71, 9, 100]
然后,可以根据需要比较它们:
>>> '100.71.9.98' < '100.71.9.100'
False
>>> [100, 71, 9, 98] < [100, 71, 9, 100]
True
>>> map(int, start_ip.split('.')) < map(int, end_ip.split('.'))
True
关于python - 验证 IP 字符串大于或小于 python 中的另一个 IP 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26428046/