python遍历一个网段的ip地址

def ip2num(ip):#ip to int num
lp = [int(x) for x in ip.split('.')]
return lp[0] << 24 | lp[1] << 16 | lp[2] << 8 | lp[3] def num2ip(num):# int num to ip
ip = ['', '', '', '']
ip[3] = (num & 0xff)
ip[2] = (num & 0xff00) >> 8
ip[1] = (num & 0xff0000) >> 16
ip[0] = (num & 0xff000000) >> 24
return '%s.%s.%s.%s' % (ip[0], ip[1], ip[2], ip[3]) def iprange(ip1,ip2):
# print ip1,ip2
# return ip1, ip2, 3
num1 = ip2num(ip1)
num2 = ip2num(ip2) tmp = num2 - num1
if tmp < 0:
return None
else:
return num1, num2, tmp

python的返回值特点简直是神的不行

上一篇:spark高级编程


下一篇:unix环境高级编程基础知识之第二篇(3)