判断一个字符串是不是ip地址

判断一个字符串是不是ip地址

python 2

#/usr/bin/env python
#-*- coding:utf-8 -*-
import sys

li=[]
ip = raw_input("please input ip:")
ip = ip.split(".")
length = len(ip)
if length != 4:
    print 'this string is not a ip address'
    sys.exit()
try:
    for i in range(0,4):
        li.append(int(ip[i]))
except:
    print 'not ip address'
    sys.exit()

for i in li:
    if isinstance(i,int):
        continue
    else:
        print 'this string is not a ip address'
        sys.exit()
for j in li:
    if  j < 0 or j > 255:
        print 'this string is not a ip addressi'
        sys.exit()
print 'is ip'

你可能感兴趣的:(python)