1
0
mirror of https://github.com/r4sas/Niflheim-api synced 2025-01-26 06:34:14 +00:00
yggdrasil-api/send-view.py

51 lines
1.3 KiB
Python
Raw Normal View History

2018-06-30 23:03:42 +02:00
import socket
import json
2018-07-03 00:38:12 +02:00
import sys
2018-06-30 23:03:42 +02:00
GETDHT = '{"request":"getDHT", "keepalive":true}'
GETSWITCHPEERS = '{"request":"getSwitchPeers"}'
SERVER = "y.yakamo.org"
2018-07-03 00:38:12 +02:00
#gives the option to get data from an external server instead and send that
#if no options given it will default to localhost instead
if len(sys.argv) == 3:
host_port = (sys.argv[1], int(sys.argv[2]))
else:
host_port = ('localhost', 9001)
2018-06-30 23:03:42 +02:00
def send_view_to_server(tosend):
if tosend:
attempts = 3
while attempts:
try:
2018-07-02 21:35:00 +02:00
conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
conn.connect((SERVER, 45671))
conn.send(tosend)
conn.close()
print "sent ok"
2018-06-30 23:03:42 +02:00
break
except:
attempts -= 1
2018-07-03 00:38:12 +02:00
def collect_dht_getswitchpeers(serport):
2018-06-30 23:03:42 +02:00
try:
ygg = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2018-07-03 00:38:12 +02:00
ygg.connect(host_port)
2018-06-30 23:03:42 +02:00
ygg.send(GETDHT)
dhtdata = json.loads(ygg.recv(1024 * 15))
ygg.send(GETSWITCHPEERS)
switchdata = json.loads(ygg.recv(1024 * 15))
temp_dict = {}
temp_dict["dhtpack"] = dhtdata
temp_dict["switchpack"] = switchdata
return json.dumps(temp_dict).encode()
except:
return None
2018-07-03 00:41:03 +02:00
send_view_to_server(collect_dht_getswitchpeers(host_port))