python 判断字符串是否是IP
正则验证IP字符串
import re
def judge_ip(one_str):
"""
正则匹配方法
判断一个字符串是否是合法IP地址
"""
compile_ip = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if compile_ip.match(one_str):
return True
else:
return False
使用方法:
judge_ip('1.1.1.1') 返回True