判断字符串是否为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'))