小编thc*_*low的帖子

从IP字符串转换为整数,在Python中向后转换

我的脚本有一点问题,我需要将表单'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`
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

python networking ip-address

43
推荐指数
8
解决办法
6万
查看次数

标签 统计

ip-address ×1

networking ×1

python ×1