20 lines
510 B
Python
20 lines
510 B
Python
import re
|
|
|
|
__HOST_PART_REGEX = re.compile(r"^([a-z0-9]|[a-z0-9][a-z0-9-]{,61}[a-z0-9])$")
|
|
__SANITIZE_REGEX = re.compile(r"[^\x20-\x7F]")
|
|
|
|
|
|
def valid_host(host: str) -> bool:
|
|
parts = host.split(".")
|
|
return (
|
|
len(parts) > 1
|
|
and len(parts[-1]) > 1
|
|
and all(__HOST_PART_REGEX.fullmatch(part) for part in parts)
|
|
and not all(part.isnumeric() for part in parts)
|
|
and len(host) < 256
|
|
)
|
|
|
|
|
|
def sanitize_string(raw: str) -> str:
|
|
return __SANITIZE_REGEX.sub("?", raw)
|