Checking for IP addresses

后端 未结 8 2075
闹比i
闹比i 2020-12-18 00:03

Are there any existing libraries to parse a string as an ipv4 or ipv6 address, or at least identify whether a string is an IP address (of either sort)?

8条回答
  •  清酒与你
    2020-12-18 00:38

    I prefer ip_interface because it handles situations both with and without prefix mask, for example, both "10.1.1.1/24" as well as simply "10.1.1.1". Needless to say, works for both v4 as well as v6

    from ipaddress import ip_interface
    ip_interface("10.1.1.1/24").ip
    ip_interface("10.1.1.1/24").ip.version
    ip_interface("10.1.1.1").ip
    ip_interface("10.1.1.1").ip.version
    

提交回复
热议问题