简介:
使用位移法将ip转为number型以及将number型转为ip,使用语言为python2.7
#!/usr/bin/env python
# coding:utf-8
def ip2num(ip):
ip = [int(x) for x in ip.split(‘.‘)]
return ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3]
print ip2num(‘10.101.101.13‘) #174417165
def num2ip(num):
return ‘%s.%s.%s.%s‘%((num & 0xff000000) >> 24,(num & 0x00ff0000) >> 16, (num & 0x0000ff00) >> 8, num & 0x000000ff)
print str(num2ip(174417165)) #10.101.101.13
有关位移算法可参考 :http://www.cnblogs.com/yyangblog/archive/2011/01/14/1935656.html
时间: 2024-10-25 22:09:50