def isIPAddress(ip, compressed=True):
"""Check if an arbitrary string is an IP address, and that it's valid.
:type ip: basestring or int
:param ip: The IP address to check.
:param boolean compressed: If True, return a string representing the
compressed form of the address. Otherwise, return an
:class:`ipaddr.IPAddress` instance.
:rtype: A :class:`ipaddr.IPAddress`, or a string, or False
:returns: The IP, as a string or a class, if it passed the
checks. Otherwise, returns False.
"""
try:
ip = ipaddr.IPAddress(ip)
except ValueError:
return False
else:
if isValidIP(ip):
if compressed:
return ip.compressed
else:
return ip
return False
评论列表
文章目录