def is_valid_hostname(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('.'))
Validate a hostname string