Convert IP to Integer and Integer To IP in Python

The following code shows how to convert IPv4/IPv6 to integer and integer to IPv4/IPv6 in Python:

from ipaddress import ip_address, IPv4Address, IPv6Address

def ip_to_integer(string_ip):
        if type(ip_address(string_ip)) is IPv4Address:
            return int(IPv4Address(string_ip))
        if type(ip_address(string_ip)) is IPv6Address:
            return int(IPv6Address(string_ip))
    except Exception as e:
        return -1

The following code shows how to convert integer IP back to IPv4/IPv6 in Python:

def integer_to_ip(int_ip):
    return ip_address(int_ip).__str__()