本文整理汇总了Python中idna.alabel方法的典型用法代码示例。如果您正苦于以下问题:Python idna.alabel方法的具体用法?Python idna.alabel怎么用?Python idna.alabel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类idna
的用法示例。
在下文中一共展示了idna.alabel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: import idna [as 别名]
# 或者: from idna import alabel [as 别名]
def encode(self, label):
if label == '':
return b''
if self.allow_pure_ascii and self.is_all_ascii(label):
return label.encode('ascii')
if not have_idna_2008:
raise NoIDNA2008
try:
if self.uts_46:
label = idna.uts46_remap(label, False, self.transitional)
return idna.alabel(label)
except idna.IDNAError as e:
raise IDNAException(idna_exception=e)
示例2: validate_domain
# 需要导入模块: import idna [as 别名]
# 或者: from idna import alabel [as 别名]
def validate_domain(self, value):
"""
Check that the hostname is valid
"""
if value[-1:] == ".":
value = value[:-1] # strip exactly one dot from the right, if present
if value == "*":
raise serializers.ValidationError("Hostname can't only be a wildcard")
labels = value.split('.')
# Let wildcards through by not trying to validate it
wildcard = True if labels[0] == '*' else False
if wildcard:
labels.pop(0)
try:
# IDN domain labels to ACE (IDNA2008)
def ToACE(x): return idna.alabel(x).decode("utf-8", "strict")
labels = list(map(ToACE, labels))
except idna.IDNAError as e:
raise serializers.ValidationError(
"Hostname does not look valid, could not convert to ACE {}: {}"
.format(value, e))
# TLD must not only contain digits according to RFC 3696
if labels[-1].isdigit():
raise serializers.ValidationError('Hostname does not look valid.')
# prepend wildcard 'label' again if removed before
if wildcard:
labels.insert(0, '*')
# recreate value using ACE'd labels
aceValue = '.'.join(labels)
if len(aceValue) > 253:
raise serializers.ValidationError('Hostname must be 253 characters or less.')
if models.Domain.objects.filter(domain=aceValue).exists():
raise serializers.ValidationError(
"The domain {} is already in use by another app".format(value))
return aceValue