actually use web server port for something. filter results by host name

This commit is contained in:
ville rantanen
2018-08-16 12:10:19 +03:00
parent 0b9cebecff
commit 6b668b8d88
2 changed files with 33 additions and 17 deletions

View File

@@ -11,6 +11,9 @@ You can send a custom text file with the update, generated with ex. `nando-desc`
Nando clients can query which other clients are online, and what their status is
(whatever you choose to update with the text data)
`nandod` can run a simple HTML server, that lists the nodes that have
returned their alive message. add the name of client at the end of URL
to filter only to that name
## Installer

27
nandod
View File

@@ -47,9 +47,12 @@ class TCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
if not opts.quiet:
print "TCP request: {0}".format(self.client_address[0])
self.data = self.request.recv(1024).strip()
method, path, _ = self.data.splitlines()[0].split()
self.path = path.lstrip("/")
self.request.send('HTTP/1.0 200 OK\r\n')
self.request.send("Content-Type: text/html\r\n\r\n")
self.request.sendall(HTMLDB.HTML_list())
self.request.sendall(HTMLDB.HTML_list(self.path))
self.request.close()
class DataBase:
@@ -128,7 +131,8 @@ class DataBase:
msg.append("{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
return TABLE_HEAD+"\n".join(msg)
def HTML_list(self):
def HTML_list(self, name):
# name = '' means do not filter by name
msg=[]
msg.append('''<html>
<head>
@@ -154,15 +158,21 @@ td, th {
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id")
msg.append('<tr><th>{0}</th><th>{1}</th><th class="right">{2}</th><th>{3}</th></tr>'.format('Host','IP','Age[d h:m:s]','Description'))
for row in self.db:
if name != '':
if name != row[0]:
continue
age=int(time.time())-row[2]
if age <ALIVE:
if age < ALIVE:
msg.append('<tr><td>{0}</td><td>{1}</td><td class="right">{2}</td><td>{3}</td></tr>'.format(row[0],row[1],humanize_time(age),row[3]))
msg.append('</table><h1>Lost</h1><table>')
msg.append('<tr><th>{0}</th><th>{1}</th><th class="right">{2}</th><th>{3}</th></tr>'.format('Host','IP','Age[d h:m:s]','Description'))
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id")
for row in self.db:
if name != '':
if name != row[0]:
continue
age=int(time.time())-row[2]
if age >=ALIVE:
if age >= ALIVE:
msg.append('<tr><td>{0}</td><td>{1}</td><td class="right">{2}</td><td>{3}</td></tr>'.format(row[0],row[1],humanize_time(age),row[3]))
msg.append('</table></body></html>')
@@ -178,9 +188,12 @@ def TCPserve():
global HTMLDB
global HTMLserver
time.sleep(2)
HTMLDB=DataBase(opts.DB)
HTMLserver = SocketServer.TCPServer((opts.HOST, opts.PORT), TCPHandler)
HTMLserver.serve_forever()
try:
HTMLDB=DataBase(opts.DB)
HTMLserver = SocketServer.TCPServer((opts.HOST, opts.WEBPORT), TCPHandler)
HTMLserver.serve_forever()
except:
sys.exit(1)
def humanize_time(secs):
mins, secs = divmod(secs, 60)