2015-08-05 19:32:53 +00:00
|
|
|
import re
|
2016-02-08 03:16:58 +00:00
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
_label_valid = re.compile(b"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
2016-10-17 03:56:46 +00:00
|
|
|
def is_valid_host(host: bytes) -> bool:
|
2015-09-20 17:40:09 +00:00
|
|
|
"""
|
2016-07-20 22:38:37 +00:00
|
|
|
Checks if a hostname is valid.
|
2015-09-20 17:40:09 +00:00
|
|
|
"""
|
2015-08-01 08:39:14 +00:00
|
|
|
try:
|
|
|
|
host.decode("idna")
|
|
|
|
except ValueError:
|
|
|
|
return False
|
2015-09-15 17:12:15 +00:00
|
|
|
if len(host) > 255:
|
|
|
|
return False
|
2016-07-20 22:38:37 +00:00
|
|
|
if host and host[-1:] == b".":
|
2015-09-15 17:12:15 +00:00
|
|
|
host = host[:-1]
|
|
|
|
return all(_label_valid.match(x) for x in host.split(b"."))
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_port(port):
|
|
|
|
return 0 <= port <= 65535
|