-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
127 lines (104 loc) · 4.23 KB
/
utils.py
File metadata and controls
127 lines (104 loc) · 4.23 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
125
126
127
# ---------------------------------------------------------------------------------------------------------------------
# A set of classes and utils for reuse in other projects.
# Being collate here for reference, copy and paste to other projects.
# ---------------------------------------------------------------------------------------------------------------------
#
import re
class DomainName(object):
"""
Class for a domain name
"""
def __init__(self, domainname):
self.domainname = domainname
self.domainname_defang = ''
self.domain_list = []
if not self.validate_domain():
raise ValueError('Invalid Domain Name supplied'.format(self.domainname))
else:
self.domain_list = self.domainname.split('.')
self.domainname_defang = "[.]".join(self.domainname.split("."))
def validate_domain(self):
regex = '^([a-z][a-z0-9+\-.]*)'
return re.search(regex, self.domainname)
def __str__(self):
return self.domainname + ',' + str(self.domain_list)
class URL(object):
"""
Base class for URL
"""
def __init__(self, url):
self.url = url
self.url_scheme = self.url.split(":")[0]
self.url_authority = re.findall("^http|https:///?([a-z][a-z0-9+\-.]:([0-9]+)", self.url)[1]
self.url_domain = re.search("^http|https:///?([a-z][a-z0-9+\-.]*)", self.url)[1]
if ":" in self.url_authority:
self.port = self.url_authority.split(":")[1]
class IPv4(object):
"""
Base class for an IPv4 address
"""
def __init__(self, ipv4addr=None, subnet=None, cidr=None):
"""
ipv4addr is considered mandatory, subnet and cidr optional
:param ipv4addr: IPv4 address value, eg 192.168.0.1.
:param subnet: subnet of IPv4 address, eg 255.255.255.0
:param cidr:
"""
self.ipv4Addr = ipv4addr
if not self.validate_ipv4():
raise ValueError('Invalid IPv4 address supplied! {}'.format(self.ipv4Addr))
else:
self.ipv4_defang = "[.]".join(self.ipv4Addr.split("."))
self.split_octet()
if subnet:
self.subnet = subnet
if self.validate_subnet():
self.cidr = (sum([bin(int(bits)).count("1") for bits in self.subnet.split(".")]))
else:
raise ValueError(' {} is not a valid subnet'.format(self.subnet))
else:
if cidr and not subnet:
self.cidr = cidr
def validate_ipv4(self):
regex = '^(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
return re.search(regex, self.ipv4Addr)
def validate_subnet(self):
regex = '^(255)\.(0|128|192|224|240|248|252|254|255)\.(0|128|192|224|240|248|252|254|255)\.(0|128|192|224|240|248|252|254|255)'
return re.search(regex, self.subnet)
def split_octet(self):
self.octets = {}
octet_list = self.ipv4Addr.split('.')
count = 0
for octet in octet_list:
self.octets[("octet_" + str(count + 1))] = octet_list[count]
count += 1
return
class EmailAddress(object):
"""
A base class for processing email addresses.
"""
def __init__(self, email_addr):
self.emailAddr = str(email_addr)
self.emailAddr_valid = True
if self.validate_emailaddress():
self.emailAddrName = self.emailAddr.split("@")[0]
self.emailAddrDomain = DomainName((self.emailAddr.split("@")[1]))
self.emailAddr_defang = "[.]".join(self.emailAddr.split("."))
else:
raise ValueError('Invalid email address supplied'.format(self.emailAddr))
def validate_emailaddress(self):
regex = '^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$'
return re.search(regex, self.emailAddr)
def __str__(self):
return self.emailAddr + "," + self.emailAddrName + str(self.emailAddrDomain2)
def main():
emailaddr = EmailAddress("peter.rabbit@microsoft.com")
print(emailaddr.__dict__)
print(emailaddr.emailAddrDomain.__dict__)
ip = IPv4('192.168.0.1', subnet="255.255.255.0")
print(ip.__dict__)
#url = URL("http://www.w3schools.com:443/python/python_regex.asp")
#print(url.__dict__)
return
if __name__ == '__main__':
main()