def is_valid_hostname(hostname):
    hostname = from_unicode(hostname).to_text()

    # stackoverflow.com/a/2532344/1707152
    if len(hostname) > 255:
        return False
    if hostname[-1] == ".":
        hostname = hostname[:-1]  # strip exactly one dot from the right
    allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
    return all(allowed.match(x) for x in hostname.split("."))
Convert domain label into IDN ACE form (handles internationalized
dataset/ETHPy150Open opendns/OpenResolve/resolverapi/util/__init__.py/is_valid_hostname