-
Notifications
You must be signed in to change notification settings - Fork 610
/
common.py
103 lines (84 loc) · 2.7 KB
/
common.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
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
# encoding: utf-8
import re
from config import *
import json
import subprocess
import logging
import requests as requests
import requests as __requests__
# from tldextract import extract, TLDExtract
from utils.fileutils import FileUtils
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
if allow_http_session:
requests = requests.Session()
def is_domain(domain):
domain_regex = re.compile(
r'(?:[A-Z0-9_](?:[A-Z0-9-_]{0,247}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,}(?<!-))\Z',
re.IGNORECASE)
return True if domain_regex.match(domain) else False
def http_request_get(url, body_content_workflow=False, allow_redirects=allow_redirects, custom_cookie=""):
try:
if custom_cookie:
headers['Cookie']=custom_cookie
result = requests.get(url,
stream=body_content_workflow,
headers=headers,
timeout=timeout,
proxies=proxies,
allow_redirects=allow_redirects,
verify=allow_ssl_verify)
return result
except Exception, e:
# return empty requests object
return __requests__.models.Response()
def http_request_post(url, payload, body_content_workflow=False, allow_redirects=allow_redirects, custom_cookie=""):
""" payload = {'key1': 'value1', 'key2': 'value2'} """
try:
if custom_cookie:
headers['Cookie']=custom_cookie
result = requests.post(url,
data=payload,
headers=headers,
stream=body_content_workflow,
timeout=timeout,
proxies=proxies,
allow_redirects=allow_redirects,
verify=allow_ssl_verify)
return result
except Exception, e:
# return empty requests object
return __requests__.models.Response()
def curl_get_content(url):
try:
cmdline = 'curl "{url}"'.format(url=url)
logging.info("subprocess call curl: {}".format(url))
run_proc = subprocess.Popen(
cmdline,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdoutput,erroutput) = run_proc.communicate()
response = {
'resp': stdoutput.rstrip(),
'err': erroutput.rstrip(),
}
return response
except Exception as e:
pass
def save_result(filename, args):
try:
fd = open(filename, 'w')
json.dump(args, fd, indent=4)
finally:
fd.close()
def read_json(filename):
if FileUtils.exists(filename):
try:
fd = open(filename, 'r')
args = json.load(fd)
return args
finally:
fd.close()
else:
return []