我的脚本有一个小问题,我需要将'xxx.xxx.xxx.xxx'格式的ip转换为整数表示,然后从这个表单返回。
def iptoint(ip):
return int(socket.inet_aton(ip).encode('hex'),16)
def inttoip(ip):
return socket.inet_ntoa(hex(ip)[2:].decode('hex'))
In [65]: inttoip(iptoint('192.168.1.1'))
Out[65]: '192.168.1.1'
In [66]: inttoip(iptoint('4.1.75.131'))
---------------------------------------------------------------------------
error Traceback (most recent call last)
/home/thc/<ipython console> in <module>()
/home/thc/<ipython console> in inttoip(ip)
error: packed IP wrong length for inet_ntoa`
有人知道怎么解决吗?
在纯python中,不使用附加模块
结果:
Python 3有ipaddress模块,它具有非常简单的转换功能:
相关问题 更多 >
编程相关推荐