makefile的高階變數賦值
阿新 • • 發佈:2018-12-20
驗證合法ip用正則太麻煩,socket模組拿來用就好,
import socket
def validate_ip_address(ipstr, proto=4):
"""
Validate specified IP address is legal IPv4 or IPv6 address
"""
family = socket.AF_INET6 if proto == 6 else socket.AF_INET
try:
socket.inet_pton(family, ipstr)#inet_aton only support ipv4
return True
except (socket.error, OSError):
return False
socket.inet_pton: Convert an IP address from its family-specific string format to a packed, binary forma
換個思維,如果能夠轉換成功,說明是合法ip