-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyn_func.py
50 lines (46 loc) · 1.29 KB
/
dyn_func.py
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
import re
# Function to validate ip address
def validate_ip(s):
a = s.split('.')
if len(a) != 4:
return False
for x in a:
if not x.isdigit():
return False
i = int(x)
if i < 0 or i > 255:
return False
return True
# Function to check hostname
def is_valid_hostname(hostname):
res = {}
a = hostname.split('.')
if "localhost" in a or "localhost." in a:
res["result"] = False
return res
if a[0].isdigit():
res["result"] = False
return res
if len(a) > 2:
if a[0].isdigit() or a[1].isdigit():
res["result"] = False
return res
if len(a) > 3:
if a[0].isdigit() or a[1].isdigit() or a[2].isdigit():
res["result"] = False
return res
if len(a) > 4:
res["result"] = False
return res
elif len(a) <= 3:
res["token_type"] = 'PQDN'
else:
res["token_type"] = 'FQDN'
if len(hostname) > 255:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
rsp = all(allowed.match(x) for x in hostname.split("."))
res["result"] = rsp
return res