ipv4的两种验证方法
import re
def check_ip4_1(ip):
result=[]
if ip.count(".")==3:
for i in ip.split("."):
try:
if i.strip().startswith("0"):
result.append(1)
elif int(i.strip())>=1 and int(i.strip())<=255:
result.append(2)
except Exception,e:
print "error",e.message
if result.count(2)==4:
return "True"
else:
return "False"
def check_ip4_2(ip):
rule1=re.compile(r"[1-2][0-9][0-9]")
rule2=re.compile(r"[1-9][1-9]")
rule3=re.compile(r"[1-9]")
result=[]
if ip.count(".")==3:
for i in ip.split("."):
try:
if i.strip().startswith("0"):
result.append(1)
elif rule1.match(i) or rule2.match(i) or rule3.match(i):
result.append(2)
except Exception,e:
print "error",e.message
if result.count(2)==4:
return "True"
else:
return "False"
if __name__=="__message__":
check_ip4_1(ip4)
check_ip4_2(ip4)