Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testIDNTLDALabels(self):
for (ulabel, alabel) in self.tld_strings:
self.assertEqual(alabel, idna.alabel(ulabel))
def validate_domain_label(label: str):
"""
Check if a given string is a valid domain label
:param label: The domain label
"""
# Just try to encode it to ASCII
try:
idna.alabel(label)
except idna.IDNAError as e:
if e.args and 'A-label' in e.args[0]:
raise ValueError('Invalid label') from None
else:
raise ValueError(e.args[0]) from None
def encode(self, label):
if label == '':
return b''
if self.allow_pure_ascii and 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)
def ToACE(x): return idna.alabel(x).decode("utf-8", "strict")
labels = list(map(ToACE, labels))