-
Notifications
You must be signed in to change notification settings - Fork 3
/
seed.py
61 lines (45 loc) · 1.43 KB
/
seed.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
import random
import threading
import time
from rmq_queue import RmqChannel, RmqQueueName
from supervisor import spawn_and_supervise
# time to initialize for other services
# time.sleep(30)
chan = RmqChannel()
chan.declare_queue(RmqQueueName.TO_WORKER)
def ips(ip_range: str):
s = ip_range.strip().split("-")
start, end = s[0], s[1]
import socket, struct
start = struct.unpack('>I', socket.inet_aton(start))[0]
end = struct.unpack('>I', socket.inet_aton(end))[0]
return [socket.inet_ntoa(struct.pack('>I', i)) for i in range(start, end)]
def send_user_ids(filename: str) -> None:
"""
sends user ids(to be multiplied by thousands) to the TO_WORKER queue
:return:
"""
result = []
with open(filename, 'r', encoding='utf-8') as f:
l = f.readlines()
random.shuffle(l)
for i in l:
print("generating ips for range", i)
result.extend(ips(i))
print("sending...")
for ip in result:
chan.send(RmqQueueName.TO_WORKER, {"ip": ip})
def start_workers() -> None:
"""
spawn worker processes
"""
# start workers
threading.Thread(target=spawn_and_supervise, args=("hik_scan.py",)).start()
pass
if chan.queue_length(RmqQueueName.TO_WORKER) == 0:
send_user_ids("ip_ranges.kz.txt")
print("spawning processes...")
start_workers()
# wait for queue to be emptied
while chan.queue_length(RmqQueueName.TO_WORKER) > 0:
time.sleep(60)