python ip地址转换

2023-01-31 04:01:53 python 地址 转换

将ip地址转换成long 和将long转换成ip

#!/usr/bin/python

import Socket, struct
def ip2long(ip):
    return struct.unpack("!L",socket.inet_aton(ip))[0]
def long2ip(longip):
    return socket.inet_ntoa(struct.pack('!L', longip))
if __name__ == '__main__':
    print('local ip address to long is %s'%ip2long('127.0.0.1'))
    print('local ip address long to ip is %s'%long2ip(2130706433))


相关文章