Checking for IP addresses

后端 未结 8 2040
闹比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.

    0 讨论(0)
  • 2020-12-18 00:16

    ipaddr -- Google's IP address manipulation package.

    Note that a proposal to include a revised version of the package in the Python standard library has recently been accepted (see PEP 3144).

    0 讨论(0)
  • 2020-12-18 00:17

    IPv4 + IPv6 solution relying only on standard library. Returns 4 or 6 or raises ValueError.

    try:
        # Python 3.3+
        import ipaddress
    
        def ip_kind(addr):
            return ipaddress.ip_address(addr).version
    
    except ImportError:
        # Fallback
        import socket
    
        def ip_kind(addr):
            try:
                socket.inet_aton(addr)
                return 4
            except socket.error: pass
            try:
                socket.inet_pton(socket.AF_INET6, addr)
                return 6
            except socket.error: pass
            raise ValueError(addr)
    
    0 讨论(0)
  • 2020-12-18 00:32

    Try

    apt-get install python-ipaddr
    

    or get the source code from here

    0 讨论(0)
  • 2020-12-18 00:36

    Yes, there is ipaddr module, that can you help to check if a string is a IPv4/IPv6 address, and to detect its version.

    import ipaddr
    import sys
    
    try:
       ip = ipaddr.IPAddress(sys.argv[1])
       print '%s is a correct IP%s address.' % (ip, ip.version)
    except ValueError:
       print 'address/netmask is invalid: %s' % sys.argv[1]
    except:
       print 'Usage : %s  ip' % sys.argv[0]
    

    But this is not a standard module, so it is not always possible to use it. You also try using the standard socket module:

    import socket
    
    try:
        socket.inet_aton(addr)
        print "ipv4 address"
    except socket.error:
        print "not ipv4 address"
    

    For IPv6 addresses you must use socket.inet_pton(socket.AF_INET6, address).

    I also want to note, that inet_aton will try to convert (and really convert it) addresses like 10, 127 and so on, which do not look like IP addresses.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题