# #
###############################################################################
+import idna
import re
import unicodedata
text = re.sub(r"-+", "-", text)
return text
+
+def is_fqdn(s):
+ """
+ Checks if a string is a valid FQDN.
+ """
+ # Decode any internationalized domains names
+ try:
+ s = idna.encode(s).decode("ascii")
+ except idna.IDNAError:
+ return False
+
+ if len(s) > 253:
+ return False
+
+ # Remove final dot, allowed in DNS but not in DB checks
+ if s.endswith("."):
+ s = s[:-1]
+
+ labels = s.split(".")
+
+ # FQDN must have at least two labels (e.g., example.com)
+ if len(labels) < 2:
+ return False
+
+ for label in labels:
+ if not 1 <= len(label) <= 63:
+ return False
+ if not label[0].isalnum() or not label[-1].isalnum():
+ return False
+ for ch in label:
+ if not (ch.isalnum() or ch == "-"):
+ return False
+
+ return True