From 90397715dcbfbe8274431bc7afdc9a2f17d12ed2 Mon Sep 17 00:00:00 2001 From: Sheng Date: Tue, 21 Aug 2018 21:55:57 +0800 Subject: [PATCH] Optimized is_valid_hostname --- webssh/utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/webssh/utils.py b/webssh/utils.py index f4611e0..b0aa15a 100644 --- a/webssh/utils.py +++ b/webssh/utils.py @@ -1,13 +1,16 @@ import ipaddress import re - try: from types import UnicodeType except ImportError: UnicodeType = str +numeric = re.compile(r'[0-9]+$') +allowed = re.compile(r'(?!-)[a-z0-9-]{1,63}(? 253: return False - labels = hostname.split(".") + labels = hostname.split('.') # the TLD must be not all-numeric - if re.match(r"[0-9]+$", labels[-1]): + if numeric.match(labels[-1]): return False - allowed = re.compile(r"(?!-)[a-z0-9-]{1,63}(?