参考代码:
# 方法1
ip = input("请输入IP:")
if len(ip.split(".")) !=4:
print("NO")
exit()
elif " " in ip.split("."):
print("NO")
exit()
else:
new_list = [int(i) for i in ip.split(".")]
for i in new_list:
if not 0 <= i < 255:
print("NO")
break
else:
print("YES")
# 方法2
import IPy
def is_ip(address):
try:
IPy.IP(address)
return "YES"
except Exception as e:
return "NO"
address = input("请输入IP:")
is_ip(address)