python 十进制 和 IP 地址互转

 1 #! /bin/python
 2 
 3 def ip2decimalism(ip):
 4     dec_value = 0
 5     v_list = ip.split('.')
 6     v_list.reverse()
 7     t = 1
 8     for v in v_list:
 9         dec_value += int(v) * t
10         t = t * (2 ** 8)
11     return dec_value
12  
13 def decimalism2ip(dec_value):
14     ip = ''
15     t = 2 ** 8
16     for _ in range(4):
17         v = dec_value % t
18         ip = '.' + str(v) + ip
19         dec_value = dec_value // t
20     ip = ip[1:]
21     return ip
22  
23 if __name__ == '__main__':
24     dec_value = ip2decimalism('0.0.0.10')
25     print(dec_value)
26 
27     ip = decimalism2ip(10)
28     print(ip)
29  

 

posted @ 2018-12-18 12:05  salami_china  阅读(1336)  评论(0编辑  收藏  举报