-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
49 lines (43 loc) · 1.14 KB
/
utils.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
49
#!/usr/bin/env python
# coding=utf-8
import re
import urlparse
def is_ip(ip_str):
ip_regx = """
^
(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
\.
(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
\.
(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
\.
(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
$
"""
result = True if re.search(ip_regx, ip_str, re.X) else False
return result
def is_url(url_str):
url_regx = """
^
(?:http(?:s)?://)? #protocol
(?:[\w]+(?::[\w]+)?@)? #user@password
([-\w]+\.)+[\w-]+(?:.)? #domain
(?::\d{2,5})? #port
(/?[-:\w;\./?%&=#]*)? #params
$
"""
result = True if re.search(url_regx, url_str, re.X) else False
return result
def get_domain_type(domain):
if is_ip(domain):
return 'ip'
elif is_url(domain):
return 'domain'
else:
return False
def get_host(domain):
o = urlparse.urlparse(domain)
return o[1]
def init_target(domain):
ret = domain if domain.find('://') != -1 else 'http://%s' % domain
return ret