首页 > 代码库 > python实现将IP地址转换为数字

python实现将IP地址转换为数字

话不多说,直接代码

ip_addr=192.168.2.10# transfer ip to intdef ip2long(ip):    ip_list=ip.split(.)    result=0    for i in range(4):  #0,1,2,3        result=result+int(ip_list[i])*256**(3-i)    return resultlong=3232236042# transfer int to ipdef long2ip(long):    floor_list=[]    yushu=long    for i in reversed(range(4)):   #3,2,1,0        res=divmod(yushu,256**i)        floor_list.append(str(res[0]))        yushu=res[1]    return ..join(floor_list)a=long2ip(long)print(a)

 

python实现将IP地址转换为数字