判断字符串是否为ip地址----python

def isIp(ip_str):
    flag = True
    if '.'  not in ip_str:
        return False
    if ip_str.count('.')!=3 :
        return False
    splitList = ip_str.split('.')
    for value in splitList:
        try:
            num = int(value)
            if num>=0 and num<=255:
                pass
            else:
                flag = False
        except:
            flag = False
    return flag

if __name__ =="__main__":
    ip1 = "test"
    print(isIp(ip1))
    print(isIp('123.3'))
    print(isIp('334.33.r.3'))
    print(isIp('334.33.1.3'))
    print(isIp('0.0.0.0'))
    print(isIp('255.255.255.255'))
    print(isIp('1.255.255.255'))
    print(isIp('1.255.255.255.22'))

 

posted @ 2020-03-15 00:41  图图图图胡图图  阅读(396)  评论(1编辑  收藏  举报