-
Notifications
You must be signed in to change notification settings - Fork 14
/
send_command.py
45 lines (43 loc) · 1.19 KB
/
send_command.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
import socket
import json
import config
import random
def send(cmd, out=False, god=False):
if god:
nodes = config.brokers
else:
nodes = config.nodes.find("nodes", {"relay":1})
random.shuffle(nodes)
if not nodes:
nodes = config.brokers
for x in nodes:
s = socket.socket()
try:
s.connect((x['ip'], x['port']))
except:
s.close()
continue
else:
s.send(json.dumps({"cmd":"get_version"}))
data = s.recv(1024)
if data == config.version:
s.close()
s = socket.socket()
try:
s.connect((x['ip'], x['port']))
except:
s.close()
continue
else:
s.send(json.dumps(cmd))
out = ""
while True:
data = s.recv(1024)
if not data:
break
out = out + data
s.close()
if out:
return out
else:
s.close()