python 十进制 和 IP 地址互转

 #! /bin/python

 def ip2decimalism(ip):
dec_value = 0
v_list = ip.split('.')
v_list.reverse()
t = 1
for v in v_list:
dec_value += int(v) * t
t = t * (2 ** 8)
return dec_value def decimalism2ip(dec_value):
ip = ''
t = 2 ** 8
for _ in range(4):
v = dec_value % t
ip = '.' + str(v) + ip
dec_value = dec_value // t
ip = ip[1:]
return ip if __name__ == '__main__':
dec_value = ip2decimalism('0.0.0.10')
print(dec_value) ip = decimalism2ip(10)
print(ip)
上一篇:InstantRun原理(2)——更新逻辑


下一篇:实战2020 | 云原生数据库加速企业数字化转型升级