-
Notifications
You must be signed in to change notification settings - Fork 15
/
cmd_addpeers.py
51 lines (37 loc) · 1.01 KB
/
cmd_addpeers.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
"""
cmd for the addpeers command.
Adds new peers to a running node
Usage:
python3 cmd_addpeers.py ip:port
python3 cmd_addpeers.py ip:port,ip:port,ip:port
if port is omitted, 5658 will be used.
"""
import connections
import json
import socks
import sys
__version__ = "0.0.1"
def add_peers(peers: dict):
s = socks.socksocket()
s.settimeout(10)
s.connect(("127.0.0.1", 5658))
# Command first
connections.send(s, "addpeers")
# addpeers expects a string, that is a json encoded dict.
connections.send(s, json.dumps(peers))
res = connections.receive(s)
return res
if __name__ == "__main__":
_, peers_string = sys.argv
peers = peers_string.split(',')
peers_dict = {}
for peer in peers:
if ':' in peer:
ip, port = peer.split(':')
else:
ip = peer
port = '5658'
peers_dict[ip] = port
res_as_dict = add_peers(peers_dict)
print("Answer (-1 means busy testing peers):")
print(json.dumps(res_as_dict, indent=2))