forked from f-prime/zCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_nodes.py
More file actions
53 lines (43 loc) · 1.18 KB
/
Copy pathget_nodes.py
File metadata and controls
53 lines (43 loc) · 1.18 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
import socket
import json
import config
import random
import sqlite3
"""
Added: 11/16/13 12:08 AM
This is the code for the "get_nodes" command, a command that will send or retrieve the global database of nodes.
{"cmd":"get_nodes"}
"""
def get_nodes(obj, data):
with open("nodes.db", 'rb') as file:
for x in file.readlines(1020):
obj.send(x)
def get_nodes_send(god=False):
node = sqlite3.connect("nodes.db")
cmd = {"cmd":"get_nodes"}
nodes = node.execute("SELECT ip, port FROM data WHERE relay=?", [True])
nodes = nodes.fetchall()
if god:
nodes = config.brokers
if not nodes:
return
random.shuffle(nodes)
for x in nodes:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps(cmd))
out = ""
while True:
data = s.recv(1024)
if data:
out = out + data
else:
break
with open("nodes.db", 'wb') as file:
file.write(out)