1 from __future__ import absolute_import, print_function
2 from future import standard_library
10 _logger = logging.getLogger('status.Handler')
13 class Server(socketserver.ThreadingMixIn, http.server.HTTPServer, object):
14 def __init__(self, config):
15 port = config.getint('Manage', 'port')
16 self.enabled = port >= 0
18 _logger.warning("Management server disabled. "+
19 "Use [Manage] config section to enable.")
22 self._tracker = tracker
23 super(Server, self).__init__(
24 (config.get('Manage', 'address'), port), Handler)
25 self._thread = threading.Thread(target=self.serve_forever)
26 self._thread.daemon = True
33 class Handler(http.server.BaseHTTPRequestHandler, object):
35 if self.path == '/status.json':
36 self.send_response(200)
37 self.send_header('Content-type', 'application/json')
39 self.wfile.write(tracker.get_json())
41 self.send_response(404)
43 def log_message(self, fmt, *args, **kwargs):
44 _logger.info(fmt, *args, **kwargs)
47 class Tracker(object):
49 self._mtx = threading.Lock()
54 return json.dumps(self._latest)
58 return self._latest.keys()
60 def update(self, updates):
62 self._latest.update(updates)