def isValidHostname(hostname): if len(hostname) > 255: return False if hostname[-1] == ".": hostname = hostname[:-1] allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) return all(allowed.match(x) for x in hostname.split(".")) def isValidIPv4Address(ipaddress): return re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ipaddress)