def validate_hostname(hostname):
    if hostname.endswith('.'):
        hostname = hostname[:-1]  # strip exactly one dot from the right, if present
    if len(hostname) < 1 or len(hostname) > 253:
        return False
    for part in hostname.split('.'):
        if len(part) < 1 or len(part) > 63 or part.startswith('-') or part.endswith('-'):
            return False
        if part.translate(hostname_allowed_chars_tbl) != '':
            return False
    return True

# fast irc casemapping validation
# part of mammon, under mammon license.
dataset/ETHPy150Open mammon-ircd/mammon/mammon/utility.py/validate_hostname/original