123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from __future__ import print_function
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- from storage import Storage
- import json
- import re
- from SocketServer import ThreadingMixIn
- class BatcaveHttpRequestHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- url = self.path
- if url == '/':
- self.respond_index()
- return
- if url == '/list':
- self.respond_list()
- return
- m = re.match(r'^/node/([a-f0-9]{12})\.json$', url)
- if m != None:
- self.respond_node(m.group(1))
- return
- self.send_error(404, 'The URL \'{0}\' was not found here.'.format(url))
- def send_nocache_headers(self):
- self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
- self.send_header('Pragma', 'no-cache')
- self.send_header('Expires', '0')
- def respond_index(self):
- storage = self.server.storage
- self.send_response(200)
- self.send_header('Content-Type', 'text/html; charset=utf-8')
- self.send_nocache_headers()
- self.end_headers()
- self.wfile.write('<!DOCTYPE html><html><head><title>BATCAVE</title></head>\n')
- self.wfile.write('<body>\n')
- self.wfile.write('<H1 title="Batman/Alfred Transmission Collection, Aggregation & Value Engine">BATCAVE</H1>\n')
- self.wfile.write('<p>Dies ist ein interner Hintergrund-Dienst. Er wird nur von anderen Diensten\n')
- self.wfile.write('angesprochen und sollte aus einer Mehrzahl von Gründen nicht öffentlich\n')
- self.wfile.write('zugänglich sein.</p>\n')
- self.wfile.write('<H2>Status</H2>\n')
- self.wfile.write('Daten: <span id="datacount" class="value">')
- self.wfile.write(len(storage.data))
- self.wfile.write('</span>\n')
- self.wfile.write('<H2>API</H2>\n')
- self.wfile.write('<p>Grundsätzlich ist das Antwort-Format JSON und alle Daten sind Live-Daten (kein Cache) die ggf. etwas Bearbeitungs-Zeit erfordern.</p>')
- self.wfile.write('<dl>\n')
- self.wfile.write('<dt><a href="/nodes.json">nodes.json</a></dt><dd>zur Verwendung mit ffmap (MACs anonymisiert)</dd>\n')
- self.wfile.write('<dt><a href="/node/ff00ff00ff00.json">/node/<id>.json</a></dt><dd><u>alle</u> vorhandenen Information zu der gewünschten Node</dd>\n')
- self.wfile.write('</dl>\n')
- self.wfile.write('</body></html>')
- def respond_list(self):
- storage = self.server.storage
- self.send_response(200)
- self.send_header('Content-Type', 'text/html')
- self.send_nocache_headers()
- self.end_headers()
- self.wfile.write('<!DOCTYPE html><html><head><title>BATCAVE</title></head>\n')
- self.wfile.write('<body>\n')
- self.wfile.write('<H1>BATCAVE - LIST</H1>\n')
- self.wfile.write('<table>\n')
- self.wfile.write('<thead><tr><th>ID</th><th>Name</th></tr></thead>\n')
- self.wfile.write('<tbody>\n')
- for nodeid in storage.data:
- nodename = storage.data[nodeid]['hostname'] if 'hostname' in storage.data[nodeid] else '<?>'
- self.wfile.write('<tr><td><a href="/node/' + nodeid + '.json">' + nodeid + '</a></td><td>' + nodename + '</td></tr>')
- self.wfile.write('</tbody>\n')
- self.wfile.write('</table>\n')
- def respond_node(self, nodeid):
- storage = self.server.storage
- if nodeid == 'ff00ff00ff00':
- self.send_response(200)
- self.send_header('Content-Type', 'text/json')
- self.send_nocache_headers()
- self.end_headers()
- self.wfile.write(json.dumps({
- 'name': 'API-Example',
- 'nodeid': nodeid,
- 'META': 'Dies ist ein minimaler Beispiel-Datensatz. Herzlichen Glückwunsch, du hast das Prinzip der API kapiert.',
- }))
- return
- if not nodeid in storage.data:
- self.send_error(404, 'No node with id \'' + nodeid + '\' present.')
- return
- self.send_response(200)
- self.send_header('Content-Type', 'text/json')
- self.send_nocache_headers()
- self.end_headers()
- self.wfile.write(json.dumps(storage.data[nodeid]))
- class ApiServer(ThreadingMixIn, HTTPServer):
- def __init__(self, endpoint, storage):
- HTTPServer.__init__(self, endpoint, BatcaveHttpRequestHandler)
- self.storage = storage
- def __str__(self):
- return 'ApiServer on {0}'.format(self.server_address)
- if __name__ == '__main__':
- dummystorage = Storage()
- server = ApiServer(('0.0.0.0', 8888), dummystorage)
- print("Server:", str(server))
- server.serve_forever()
|