-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathhostname.py
More file actions
124 lines (106 loc) · 3.62 KB
/
Copy pathhostname.py
File metadata and controls
124 lines (106 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Hostname."""
# -*- coding: utf-8 -*-
# standard
from functools import lru_cache
import re
# local
from .ip_address import ipv6, ipv4
from .utils import validator
from .domain import domain
@lru_cache
def _port_regex():
"""Port validation regex."""
return re.compile(
r"^\:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|"
+ r"6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})$",
)
@lru_cache
def _simple_hostname_regex():
"""Simple hostname validation regex."""
return re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$")
def _port_validator(value: str):
"""Returns host segment if port is valid."""
if value.count("]:") == 1:
# with ipv6
host_seg, port_seg = value.rsplit(":", 1)
if _port_regex().match(f":{port_seg}"):
return host_seg.lstrip("[").rstrip("]")
if value.count(":") == 1:
# with ipv4 or simple hostname
host_seg, port_seg = value.rsplit(":", 1)
if _port_regex().match(f":{port_seg}"):
return host_seg
return None
@validator
def hostname(
value: str,
/,
*,
skip_ipv6_addr: bool = False,
skip_ipv4_addr: bool = False,
may_have_port: bool = True,
maybe_simple: bool = True,
rfc_1034: bool = False,
rfc_2782: bool = False,
):
"""Return whether or not given value is a valid hostname.
Examples:
>>> hostname("ubuntu-pc:443")
# Output: True
>>> hostname("this-pc")
# Output: True
>>> hostname("xn----gtbspbbmkef.xn--p1ai:65535")
# Output: True
>>> hostname("_example.com")
# Output: True
>>> hostname("123.5.77.88:31000")
# Output: True
>>> hostname("12.12.12.12")
# Output: True
>>> hostname("[::1]:22")
# Output: True
>>> hostname("dead:beef:0:0:0:0000:42:1")
# Output: True
>>> hostname("[0:0:0:0:0:ffff:1.2.3.4]:-65538")
# Output: ValidationFailure(func=hostname, ...)
>>> hostname("[0:&:b:c:@:e:f::]:9999")
# Output: ValidationFailure(func=hostname, ...)
Args:
value:
Hostname string to validate.
skip_ipv6_addr:
When hostname string cannot be an IPv6 address.
skip_ipv4_addr:
When hostname string cannot be an IPv4 address.
may_have_port:
Hostname string may contain port number.
maybe_simple:
Hostname string maybe only hyphens and alpha-numerals.
rfc_1034:
Allow trailing dot in domain/host name.
Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034).
rfc_2782:
Domain/Host name is of type service record.
Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).
Returns:
(Literal[True]):
If `value` is a valid hostname.
(ValidationFailure):
If `value` is an invalid hostname.
> *New in version 0.21.0*.
"""
if not value:
return False
if may_have_port and (host_seg := _port_validator(value)):
return (
(_simple_hostname_regex().match(host_seg) if maybe_simple else False)
or domain(host_seg, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
or (False if skip_ipv4_addr else ipv4(host_seg, cidr=False))
or (False if skip_ipv6_addr else ipv6(host_seg, cidr=False))
)
return (
(_simple_hostname_regex().match(value) if maybe_simple else False)
or domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
or (False if skip_ipv4_addr else ipv4(value, cidr=False))
or (False if skip_ipv6_addr else ipv6(value, cidr=False))
)