Development
This commit is contained in:
parent
a4dcf4705c
commit
e519660ea5
3 changed files with 43 additions and 4 deletions
|
|
@ -128,6 +128,20 @@ def domainname(value, max_len=253):
|
|||
"""Hostname or domain: letters, digits, hyphens, dots. Lowercased."""
|
||||
return _strip(value.lower(), r'[^a-z0-9\-.]', max_len)
|
||||
|
||||
_HOSTNAME_RE = re.compile(r'^[a-z0-9]([a-z0-9_\-]*[a-z0-9])?$')
|
||||
|
||||
def hostname(value, max_len=253):
|
||||
"""Network hostname: letters, digits, hyphens, underscores. Must start and end with
|
||||
alphanumeric. No consecutive hyphens or underscores. Returns lowercase if valid, None if not."""
|
||||
s = str(value).strip().lower()
|
||||
if not s or len(s) > max_len:
|
||||
return None
|
||||
if re.search(r'[-_]{2,}', s):
|
||||
return None
|
||||
if not _HOSTNAME_RE.match(s):
|
||||
return None
|
||||
return s
|
||||
|
||||
def domainlist(lines):
|
||||
"""Sanitize a list of domain name strings, returning only non-empty results."""
|
||||
return [h for v in lines if (h := domainname(v))]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue