-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathdomain.py
More file actions
63 lines (53 loc) · 1.77 KB
/
domain.py
File metadata and controls
63 lines (53 loc) · 1.77 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
"""Domain."""
# -*- coding: utf-8 -*-
# standard
import re
# local
from .utils import validator
@validator
def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):
"""Return whether or not given value is a valid domain.
Examples:
>>> domain('example.com')
# Output: True
>>> domain('example.com/')
# Output: ValidationFailure(func=domain, ...)
>>> # Supports IDN domains as well::
>>> domain('xn----gtbspbbmkef.xn--p1ai')
# Output: True
Args:
value:
Domain string to validate.
rfc_1034:
Allow trailing dot in domain name.
Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034).
rfc_2782:
Domain name is of type service record.
Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).
Returns:
(Literal[True]):
If `value` is a valid domain name.
(ValidationFailure):
If `value` is an invalid domain name.
Note:
- *In version 0.10.0*:
- Added support for internationalized domain name (IDN) validation.
> *New in version 0.9.0*.
"""
if not value:
return False
try:
return not re.search(r"\s", value) and re.match(
# First character of the domain
rf"^(?:[a-zA-Z0-9{'_'if rfc_2782 else ''}]"
# Sub domain + hostname
+ r"(?:[a-zA-Z0-9-_]{0,61}[A-Za-z0-9])?\.)"
# First 61 characters of the gTLD
+ r"+[A-Za-z0-9][A-Za-z0-9-_]{0,61}"
# Last character of the gTLD
+ rf"[A-Za-z]{r'.$' if rfc_1034 else r'$'}",
value.encode("idna").decode("utf-8"),
re.IGNORECASE,
)
except UnicodeError:
return False