forked from cybersecurity-team/TorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
114 lines (89 loc) · 2.87 KB
/
Copy pathutils.py
File metadata and controls
114 lines (89 loc) · 2.87 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
"""
Provides essential utilites for the rest of TorBot app.
"""
import os
from queue import Queue
from threading import Thread
import requests
from requests.exceptions import HTTPError
# ALGORITHM UTILITY FUNCTIONS
def process_data(data_queue, data_stack, process, *args):
"""
Processes tasks using by grabbing threads from queue
Args:
data_queue (queue.Queue): Contains tasks in FIFO data structure.
data_processor (function): Function to be executed on task and args.
data_args (tuple): Contains arguments for tasks.
"""
while True:
data = data_queue.get()
if isinstance(data, list):
for single_inst in data:
if args:
result = process(single_inst, args)
else:
result = process(single_inst)
elif args:
result = process(data, args)
else:
result = process(data)
if result:
data_stack.append(result)
data_queue.task_done()
def multi_thread(data, data_function, *args):
"""
Start threads with function to process data and arguments then
process the data in FIFO order.
Args:
data (list): Lists of values that you'd like to operate on.
data_function (function): Function that you would like to use
for processsing.
args (tuple): Arguments for function.
Returns:
(list): List of processed data elements.
"""
data_queue = Queue(len(data)*2)
ret_stack = list()
for _ in data:
data_args = (data_queue, ret_stack, data_function, *args)
thd = Thread(target=process_data, args=data_args)
thd.daemon = True
thd.start()
for obj in data:
data_queue.put(obj)
data_queue.join()
return ret_stack
# Networking functions
def get_url_status(url, headers=False):
"""
Uses GET request to check if website exists.
*NOTE: May look into changing this to HEAD requests to improve perf
Args:
url (str): URL to be tested.
Return:
(response object): Return response object from connection
if successful.
(int): Return 0 if connection not successful.
"""
try:
if headers:
resp = requests.get(url, headers=headers)
else:
resp = requests.get(url)
resp.raise_for_status()
return resp
except (ConnectionError, HTTPError):
return 0
def find_file(name, path):
"""Search for file within specific dir and any child dirs.
Args:
name (str): Filename to be searched for.
path (str): Dir path to search for file.
Returns:
str: Full path of found file (if found).
bool: If file not found, returns false.
"""
for root, _, files in os.walk(path):
if name in files:
return os.path.join(root, name)
return False