summaryrefslogtreecommitdiffstats
path: root/venv/lib/python3.9/site-packages/validators/domain.py
diff options
context:
space:
mode:
authornoptuno <repollo.marrero@gmail.com>2023-04-28 02:29:30 +0200
committernoptuno <repollo.marrero@gmail.com>2023-04-28 02:29:30 +0200
commit355dee533bb34a571b9367820a63cccb668cf866 (patch)
tree838af886b4fec07320aeb10f0d1e74ba79e79b5c /venv/lib/python3.9/site-packages/validators/domain.py
parentadded pyproject.toml file (diff)
downloadgpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.gz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.bz2
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.lz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.xz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.zst
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.zip
Diffstat (limited to 'venv/lib/python3.9/site-packages/validators/domain.py')
-rw-r--r--venv/lib/python3.9/site-packages/validators/domain.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/venv/lib/python3.9/site-packages/validators/domain.py b/venv/lib/python3.9/site-packages/validators/domain.py
new file mode 100644
index 00000000..d9bf44f0
--- /dev/null
+++ b/venv/lib/python3.9/site-packages/validators/domain.py
@@ -0,0 +1,54 @@
+import re
+
+from .utils import validator
+
+pattern = re.compile(
+ r'^(?:[a-zA-Z0-9]' # First character of the domain
+ r'(?:[a-zA-Z0-9-_]{0,61}[A-Za-z0-9])?\.)' # Sub domain + hostname
+ r'+[A-Za-z0-9][A-Za-z0-9-_]{0,61}' # First 61 characters of the gTLD
+ r'[A-Za-z]$' # Last character of the gTLD
+)
+
+
+def to_unicode(obj, charset='utf-8', errors='strict'):
+ if obj is None:
+ return None
+ if not isinstance(obj, bytes):
+ return str(obj)
+ return obj.decode(charset, errors)
+
+
+@validator
+def domain(value):
+ """
+ Return whether or not given value is a valid domain.
+
+ If the value is valid domain name this function returns ``True``, otherwise
+ :class:`~validators.utils.ValidationFailure`.
+
+ Examples::
+
+ >>> domain('example.com')
+ True
+
+ >>> domain('example.com/')
+ ValidationFailure(func=domain, ...)
+
+
+ Supports IDN domains as well::
+
+ >>> domain('xn----gtbspbbmkef.xn--p1ai')
+ True
+
+ .. versionadded:: 0.9
+
+ .. versionchanged:: 0.10
+
+ Added support for internationalized domain name (IDN) validation.
+
+ :param value: domain string to validate
+ """
+ try:
+ return pattern.match(to_unicode(value).encode('idna').decode('ascii'))
+ except (UnicodeError, AttributeError):
+ return False