isActiveIPv4(ip)
参数 ip,字符串
用途,判断ip字符串是否为合法的IPv4地址
依赖模块,ipaddress
返回值,判定为是,返回 字符串 'Y'
返回值,判定为否,返回 字符串 'N'
user@server:~$ cat tmp1348.py
import ipaddress
def isActiveIPv4(ip):
"""
isActiveIPv4(ip)
参数 ip,字符串
用途,判断ip字符串是否为合法的IPv4地址
依赖模块,ipaddress
返回值,判定为是,返回 字符串 'Y'
返回值,判定为否,返回 字符串 'N'
"""
try:
vBLACKIPs = ['0.0.0.0','255.255.255.255']
if ipaddress.IPv4Address(ip) and ip not in vBLACKIPs:
return 'Y'
else:
return 'N'
except Exception as e:
return 'N'
data1 = 'datastring'
data2 = '127.0.0.1'
data3 = '192.168.1.1'
data4 = '0.0.0.0'
v1 = isActiveIPv4(ip = data1)
v2 = isActiveIPv4(ip = data2)
v3 = isActiveIPv4(ip = data3)
v4 = isActiveIPv4(ip = data4)
print(f"{data1} - {v1}")
print(f"{data2} - {v2}")
print(f"{data3} - {v3}")
print(f"{data4} - {v4}")
user@server:~$ python3 tmp1348.py
datastring - N
127.0.0.1 - Y
192.168.1.1 - Y
0.0.0.0 - N
user@server:~$
云乞讨