diff --git a/webserver.py b/webserver.py index 274a4dd..af6278e 100644 --- a/webserver.py +++ b/webserver.py @@ -5,10 +5,12 @@ import json import logging import os -from http.server import BaseHTTPRequestHandler, HTTPServer -from socketserver import ThreadingMixIn from logging.handlers import TimedRotatingFileHandler +import tornado.ioloop +import tornado.web +import asyncio + from external_webpage.request_handler_utils import get_detailed_state_of_specific_instrument, \ get_summary_details_of_all_instruments, get_instrument_and_callback from external_webpage.web_scrapper_manager import WebScrapperManager @@ -24,22 +26,23 @@ HOST, PORT = '', 60000 -class MyHandler(BaseHTTPRequestHandler): +class MyHandler(tornado.web.RequestHandler): """ Handle for web calls for Json Borne """ - def do_GET(self): + def get(self): """ This is called by BaseHTTPRequestHandler every time a client does a GET. The response is written to self.wfile """ + path = self.request.uri instrument = "Not set" try: - instrument, callback = get_instrument_and_callback(self.path) + instrument, callback = get_instrument_and_callback(path) # Debug is only needed when debugging - logger.debug("Connection from " + str(self.client_address) + " looking at " + str(instrument)) + logger.debug("Connection from {} looking at {}".format(self.request.remote_ip, instrument)) with scraped_data_lock: if instrument == "ALL": @@ -53,22 +56,21 @@ def do_GET(self): try: ans_as_json = str(json.dumps(ans)) except Exception as err: - raise ValueError("Unable to convert answer data to JSON: %s" % err.message) + raise ValueError("Unable to convert answer data to JSON: {}".format(err)) response = "{}({})".format(callback, ans_as_json) - self.send_response(200) - self.send_header('Content-type', 'text/html') - self.end_headers() - self.wfile.write(response.encode("utf-8")) + self.set_status(200) + self.set_header('Content-type', 'text/html') + self.write(response.encode("utf-8")) except ValueError as e: logger.exception("Value Error when getting data from {} for {}: {}".format( - self.client_address, instrument, e)) - self.send_response(400) + self.request.remote_ip, instrument, e)) + self.set_status(400) except Exception as e: logger.exception("Exception when getting data from {} for {}: {}".format( - self.client_address, instrument, e)) - self.send_response(404) + self.request.remote_ip, instrument, e)) + self.set_status(404) def log_message(self, format, *args): """ By overriding this method and doing nothing we disable writing to console @@ -76,10 +78,6 @@ def log_message(self, format, *args): return -class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): - """Handle requests in a separate thread.""" - - if __name__ == '__main__': # It can sometime be useful to define a local instrument list to add/override the instrument list do this here # E.g. to add local instrument local_inst_list = {"localhost": "localhost"} @@ -87,11 +85,15 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): web_manager = WebScrapperManager(local_inst_list=local_inst_list) web_manager.start() - server = ThreadedHTTPServer(('', PORT), MyHandler) + # As documented at https://github.com/tornadoweb/tornado/issues/2608 + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) try: - while True: - server.serve_forever() + application = tornado.web.Application([ + (r"/", MyHandler), + ]) + application.listen(PORT) + tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt: print("Shutting down") web_manager.stop()