解析域名并判断内外网ip

import socket
import ipaddress
from urllib.parse import urlparse

def res(url):
    try:
        data = urlparse(url)
        domain = urlparse(url).netloc
        domain = domain.split(':')[0]
        res = socket.getaddrinfo(domain, None)
        print(res)
        ip = res[0][4][0]
        return ip
    except Exception as e:
        print(e)
        return "未知"


def check_ip_valid(ip):
    try:
        ipaddress.ip_address(ip.strip())
        return True
    except:
        return False


def is_inner(ip):
    try:
        return ipaddress.ip_address(ip.strip()).is_private
    except:
        return False


def check_domain(url):
    IP = res(url)
    if IP == "未知":
        return "-"
    if is_inner(IP):
        return "内网域名"
    else:
        return "外网域名"

 

posted @ 2022-06-04 12:14  Οo白麒麟оΟ  阅读(220)  评论(0编辑  收藏  举报