import re
def is_valid_hostname(hostname):
if len(hostname) > 255:
return False
if hostname[-1] == “.”:
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile(“(?!-)[A-Zd-]{1,63}(?<!-)$”, re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split(“.”))
确保每个细分市场
- 包含至少一个字符,最多63个字符
- 仅包含允许的字符
- 不以连字符开头或结尾。
它还避免了双负号(
notdisallowed),并且如果
hostname以结束
.,也可以。如果
hostname结尾超过一个点,它将(并且应该)失败。



