server.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  5. from storage import Storage
  6. import json
  7. import re
  8. from SocketServer import ThreadingMixIn
  9. class BatcaveHttpRequestHandler(BaseHTTPRequestHandler):
  10. def do_GET(self):
  11. url = self.path
  12. if url == '/':
  13. self.respond_index()
  14. return
  15. if url == '/list':
  16. self.respond_list()
  17. return
  18. m = re.match(r'^/node/([a-f0-9]{12})\.json$', url)
  19. if m != None:
  20. self.respond_node(m.group(1))
  21. return
  22. self.send_error(404, 'The URL \'{0}\' was not found here.'.format(url))
  23. def send_nocache_headers(self):
  24. self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
  25. self.send_header('Pragma', 'no-cache')
  26. self.send_header('Expires', '0')
  27. def respond_index(self):
  28. storage = self.server.storage
  29. self.send_response(200)
  30. self.send_header('Content-Type', 'text/html; charset=utf-8')
  31. self.send_nocache_headers()
  32. self.end_headers()
  33. self.wfile.write('<!DOCTYPE html><html><head><title>BATCAVE</title></head>\n')
  34. self.wfile.write('<body>\n')
  35. self.wfile.write('<H1 title="Batman/Alfred Transmission Collection, Aggregation & Value Engine">BATCAVE</H1>\n')
  36. self.wfile.write('<p>Dies ist ein interner Hintergrund-Dienst. Er wird nur von anderen Diensten\n')
  37. self.wfile.write('angesprochen und sollte aus einer Mehrzahl von Gr&uuml;nden nicht &ouml;ffentlich\n')
  38. self.wfile.write('zug&auml;nglich sein.</p>\n')
  39. self.wfile.write('<H2>Status</H2>\n')
  40. self.wfile.write('Daten: <span id="datacount" class="value">')
  41. self.wfile.write(len(storage.data))
  42. self.wfile.write('</span>\n')
  43. self.wfile.write('<H2>API</H2>\n')
  44. 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>')
  45. self.wfile.write('<dl>\n')
  46. self.wfile.write('<dt><a href="/nodes.json">nodes.json</a></dt><dd>zur Verwendung mit ffmap (MACs anonymisiert)</dd>\n')
  47. self.wfile.write('<dt><a href="/node/ff00ff00ff00.json">/node/&lt;id&gt;.json</a></dt><dd><u>alle</u> vorhandenen Information zu der gewünschten Node</dd>\n')
  48. self.wfile.write('</dl>\n')
  49. self.wfile.write('</body></html>')
  50. def respond_list(self):
  51. storage = self.server.storage
  52. self.send_response(200)
  53. self.send_header('Content-Type', 'text/html')
  54. self.send_nocache_headers()
  55. self.end_headers()
  56. self.wfile.write('<!DOCTYPE html><html><head><title>BATCAVE</title></head>\n')
  57. self.wfile.write('<body>\n')
  58. self.wfile.write('<H1>BATCAVE - LIST</H1>\n')
  59. self.wfile.write('<table>\n')
  60. self.wfile.write('<thead><tr><th>ID</th><th>Name</th></tr></thead>\n')
  61. self.wfile.write('<tbody>\n')
  62. for nodeid in storage.data:
  63. nodename = storage.data[nodeid]['hostname'] if 'hostname' in storage.data[nodeid] else '&lt;?&gt;'
  64. self.wfile.write('<tr><td><a href="/node/' + nodeid + '.json">' + nodeid + '</a></td><td>' + nodename + '</td></tr>')
  65. self.wfile.write('</tbody>\n')
  66. self.wfile.write('</table>\n')
  67. def respond_node(self, nodeid):
  68. storage = self.server.storage
  69. if nodeid == 'ff00ff00ff00':
  70. self.send_response(200)
  71. self.send_header('Content-Type', 'text/json')
  72. self.send_nocache_headers()
  73. self.end_headers()
  74. self.wfile.write(json.dumps({
  75. 'name': 'API-Example',
  76. 'nodeid': nodeid,
  77. 'META': 'Dies ist ein minimaler Beispiel-Datensatz. Herzlichen Glückwunsch, du hast das Prinzip der API kapiert.',
  78. }))
  79. return
  80. if not nodeid in storage.data:
  81. self.send_error(404, 'No node with id \'' + nodeid + '\' present.')
  82. return
  83. self.send_response(200)
  84. self.send_header('Content-Type', 'text/json')
  85. self.send_nocache_headers()
  86. self.end_headers()
  87. self.wfile.write(json.dumps(storage.data[nodeid]))
  88. class ApiServer(ThreadingMixIn, HTTPServer):
  89. def __init__(self, endpoint, storage):
  90. HTTPServer.__init__(self, endpoint, BatcaveHttpRequestHandler)
  91. self.storage = storage
  92. def __str__(self):
  93. return 'ApiServer on {0}'.format(self.server_address)
  94. if __name__ == '__main__':
  95. dummystorage = Storage()
  96. server = ApiServer(('0.0.0.0', 8888), dummystorage)
  97. print("Server:", str(server))
  98. server.serve_forever()