python判断是否是合法的ip地址

def ip_fun(ip_str: str):
    ip_list = ip_str.split(".")
    flag = True
    for ip in ip_list:
        if ip.isdigit() and len(ip_list) == 4 and 0 <= int(ip) <= 255:
            continue
        else:
            flag = False
            break
    if flag:
        print("是ip地址")
        return
    print("不是ip地址")
ip_fun('172.0.0.256')

 

posted @ 2023-03-16 16:51  dyjnicole  阅读(28)  评论(0编辑  收藏  举报