Checking for IP addresses

后端 未结 8 2062
闹比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:14

    For IPv4, you can use

    socket.inet_aton(some_string)
    

    If it throws an exception, some_string is not a valid ip address

    For IPv6, you can use:

    socket.inet_pton(socket.AF_INET6, some_string)
    

    Again, it throws an exception, if some_string is not a valid address.

提交回复
热议问题