Convert an IP string to a number and vice versa

前端 未结 9 1059
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 03:01

How would I use python to convert an IP address that comes as a str to a decimal number and vice versa?

For example, for the IP 186.99.109.000 &l

9条回答
  •  无人及你
    2020-11-28 03:56

    def ip2Hex(ip = None):
        '''Returns IP in Int format from Octet form'''
        #verifyFormat(ip)
        digits=ip.split('.')
        numericIp=0
        count=0
        for num in reversed(digits):
            print "%d " % int(num)
            numericIp += int(num) * 256 **(count)
            count +=1
        print "Numeric IP:",numericIp
        print "Numeric IP Hex:",hex(numericIp)
    
    ip2Hex('192.168.192.14')
    ip2Hex('1.1.1.1')
    ip2Hex('1.0.0.0')
    

提交回复
热议问题