Convert an IP string to a number and vice versa

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

    Since Python 3.3 there is the ipaddress module that does exactly this job among others: https://docs.python.org/3/library/ipaddress.html. Backports for Python 2.x are also available on PyPI.

    Example usage:

    import ipaddress
    
    ip_in_int = int(ipaddress.ip_address('192.168.1.1'))
    ip_in_hex = hex(ipaddress.ip_address('192.168.1.1'))
    

提交回复
热议问题