Convert an IP string to a number and vice versa

前端 未结 9 1025
隐瞒了意图╮
隐瞒了意图╮ 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:58

    Here's one

    def ipv4_to_int(ip):
        octets = ip.split('.')
        count = 0
        for i, octet in enumerate(octets):
            count += int(octet) << 8*(len(octets)-(i+1))
        return count
    

提交回复
热议问题