import ipaddress


def get_ips(ip=None, sep=None, fn='IPv4Address'):
    """
    检查是否为 IP 地址/网段/接口, 清除每个 IP 的空白, 返回 IP 字符串或 IP 列表

    e.g.::

        # 127.0.0.1
        get_ips(' 127.0.0.1   ')

        # 127.0.0.1
        get_ips(0x7f000001)

        # ['127.0.0.1', '8.8.8.8']
        get_ips('127.0.0.1, 8.8.8.8,, 10a, 7,,127.0.0.1,::1 ', ',')

        # ['::1', '2001:db8::']
        get_ips('127.0.0.1, 8.8.8.8,, 10a, 7,,127.0.0.1,::1 ,2001:db8::', ',', 'IPv6Address')

        # 0.0.0.10
        get_ips(10)

        # None
        get_ips('10')

        # 172.16.0.0/12
        get_ips('172.16.0.0/12', fn='IPv4Network')

        # 172.16.0.1/12
        get_ips('172.16.0.1/12', fn='IPv4Interface')

        # ['127.0.0.1/31', '8.8.8.8/24', '127.0.1.0/24']
        get_ips('127.0.0.1/31, 8.8.8.8/24,, 10a, 7,,127.0.1.0/24,::1 ', ',' , fn='IPv4Interface')

    :param ip: str, IP 地址
    :param sep: str, 指定分隔符时返回 list
    :param fn: str, ip_address / ip_network / ip_interface
    :return: str / list
    """
    if not ip:
        return None

    if sep:
        ret = []
        for x in ip.split(sep):
            ip_str = get_ips(x, fn=fn)
            if ip_str and ip_str not in ret:
                ret.append(ip_str)
    else:
        if isinstance(ip, str):
            ip = ip.strip()
        try:
            ret = str(getattr(ipaddress, fn)(ip))
        except Exception:
            ret = None

    return ret