python ip地址转换

发布时间:2019-09-04 06:54:50编辑:auto阅读(2091)

    将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))


关键字