Browse Source

some minor fixes

develop
yakamok 6 years ago committed by GitHub
parent
commit
5c3a33e0b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      vserv.py

40
vserv.py

@ -39,44 +39,44 @@ def isdatabase(db_path):
if not os.path.exists(db_path): if not os.path.exists(db_path):
os.makedirs(db_path) os.makedirs(db_path)
try: try:
conn = sqlite3.connect(db_path + 'yggindex.db') dbconn = sqlite3.connect(db_path + 'yggindex.db')
c = conn.cursor() c = dbconn.cursor()
c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\
dt datetime default (strftime('%s','now')))''') dt datetime default (strftime('%s','now')))''')
c.execute('''create table timeseries(max varchar(45),\ c.execute('''create table timeseries(max varchar(45),\
dt datetime default (strftime('%s','now')))''') dt datetime default (strftime('%s','now')))''')
c.execute('''create table contrib(ipv6 varchar(45) UNIQUE,\ c.execute('''create table contrib(ipv6 varchar(45) UNIQUE,\
ut unixtime default (strftime('%s','now')))''') ut unixtime default (strftime('%s','now')))''')
conn.commit() dbconn.commit()
except Error as e: except Error as e:
print(e) print(e)
finally: finally:
conn.close() dbconn.close()
else: else:
print("found database will not create a new one") print("found database will not create a new one")
def insert_new_entry(db_path, ipv6, coords): def insert_new_entry(db_path, ipv6, coords):
try: try:
conn = sqlite3.connect(db_path + "yggindex.db") dbconn = sqlite3.connect(db_path + "yggindex.db")
c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ dbconn.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\
(ipv6, coords)) (ipv6, coords))
conn.commit() dbconn.commit()
conn.close() dbconn.close()
except Error as e: except Error as e:
print e print e
def contrib_entry(db_path, ipv6): def contrib_entry(db_path, ipv6):
try: try:
conn = sqlite3.connect(db_path + "yggindex.db") dbconn = sqlite3.connect(db_path + "yggindex.db")
c.execute('''INSERT OR REPLACE INTO contrib(ipv6) VALUES(''' + "'"\ dbconn.execute('''INSERT OR REPLACE INTO contrib(ipv6) VALUES(''' + "'"\
+ ipv6 + "'" + ''')''') + ipv6 + "'" + ''')''')
conn.commit() dbconn.commit()
conn.close() dbconn.close()
except Error as e: except Error as e:
print e print e
def error_check_insert_into_db(dht, switchpeers, addr): def error_check_insert_into_db(dht, switchpeers, ipv6):
try: try:
if dht.get("status") == "success": if dht.get("status") == "success":
for x, y in dht["response"]["dht"].iteritems(): for x, y in dht["response"]["dht"].iteritems():
@ -88,17 +88,17 @@ def error_check_insert_into_db(dht, switchpeers, addr):
if valid_ipv6_check(x[1]["ip"]) and check_coords(x[1]["coords"]): if valid_ipv6_check(x[1]["ip"]) and check_coords(x[1]["coords"]):
insert_new_entry(DB_PATH, x[1]["ip"], x[1]["coords"]) insert_new_entry(DB_PATH, x[1]["ip"], x[1]["coords"])
contrib_entry(addr) contrib_entry(DB_PATH, ipv6)
except: except:
print"error in json file, aborting" print"error in json file, aborting"
def proccess_incoming_data(datty, addr): def proccess_incoming_data(datty, ipv6):
print str(time.time()) + " " + addr print str(time.time()) + " " + str(ipv6)
try: try:
ddata = datty.recv(1024 * 20) ddata = datty.recv(1024 * 20)
data = json.loads(ddata.decode()) data = json.loads(ddata.decode())
error_check_insert_into_db(data["dhtpack"], data["switchpack"]) error_check_insert_into_db(data["dhtpack"], data["switchpack"], ipv6)
except: except:
print "ignoring, data was not json" print "ignoring, data was not json"
@ -112,6 +112,6 @@ conn.listen(30)
while True: while True:
try: try:
dataraw, addr = conn.accept() dataraw, addr = conn.accept()
thread.start_new_thread(proccess_incoming_data, (dataraw, addr)) thread.start_new_thread(proccess_incoming_data, (dataraw, addr[0]))
except Exception: except Exception:
print "bloop" print "bloop"

Loading…
Cancel
Save