python ip格式转化为long型
Nov272008
作者:u2 发布:2008-11-27 20:46 分类:python python ip格式转化为long型已关闭评论
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#ip转化为long型格式
def iptolong(ipaddr):
data = 0L
ip = ipaddr.split(‘.’)
for i in range(0,len(ip)):
if 0 <= long(ip[i]) < 256:
data = data+long(ip[i])*(256**(3-i))
else:
data = 0L
break
return data
#long型转化成ip格式
def longtoip(iplong):
i_4 = iplong % 256
i_3 = (iplong – i_4)/256 % 256
i_2 = (iplong – i_3*256 – i_4)/256**2 % 256
i_1 = (iplong – i_2*256*256 – i_3*256 – i_4)/256**3 % 256
ip = str(i_1)+’.’+str(i_2)+’.’+str(i_3)+’.’+str(i_4)
return ip
本文固定链接: https://www.2hei.net/2008/11/27/python_ip_to_long/ | 2hei.net
最活跃的读者