From fce4cf0f176870af405c72cb6bcacacc6f00ed06 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Fri, 19 Apr 2019 20:53:41 +0200 Subject: [PATCH] statics server service created --- pype/services/statics_server/__init__.py | 5 ++ .../services/statics_server/statics_server.py | 78 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 pype/services/statics_server/__init__.py create mode 100644 pype/services/statics_server/statics_server.py diff --git a/pype/services/statics_server/__init__.py b/pype/services/statics_server/__init__.py new file mode 100644 index 0000000000..4b2721b18b --- /dev/null +++ b/pype/services/statics_server/__init__.py @@ -0,0 +1,5 @@ +from .statics_server import StaticsServer + + +def tray_init(tray_widget, main_widget): + return StaticsServer() diff --git a/pype/services/statics_server/statics_server.py b/pype/services/statics_server/statics_server.py new file mode 100644 index 0000000000..8fb9990355 --- /dev/null +++ b/pype/services/statics_server/statics_server.py @@ -0,0 +1,78 @@ +import os +import socket +import http.server +import socketserver + +from Qt import QtCore +from pypeapp import config, Logger + + +DIRECTORY = os.path.sep.join([os.environ['PYPE_MODULE_ROOT'], 'res']) + + +class Handler(http.server.SimpleHTTPRequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, directory=DIRECTORY, **kwargs) + + +class StaticsServer(QtCore.QThread): + """ Measure user's idle time in seconds. + Idle time resets on keyboard/mouse input. + Is able to emit signals at specific time idle. + """ + + def __init__(self): + super(StaticsServer, self).__init__() + self._is_running = False + self._failed = False + self.log = Logger().get_logger(self.__class__.__name__) + try: + self.presets = config.get_presets().get( + 'services', {}).get('statics_server') + except Exception: + self.presets = {'default_port': 8010, 'exclude_ports': []} + + self.port = self.find_port() + + def tray_start(self): + self.start() + + @property + def is_running(self): + return self._is_running + + @property + def failed(self): + return self._failed + + def stop(self): + self._is_running = False + + def run(self): + self._is_running = True + try: + with socketserver.TCPServer(("", self.port), Handler) as httpd: + while self._is_running: + httpd.handle_request() + except Exception: + self._failed = True + self._is_running = False + + def find_port(self): + start_port = self.presets['default_port'] + exclude_ports = self.presets['exclude_ports'] + found_port = None + # port check takes time so it's lowered to 100 ports + for port in range(start_port, start_port+100): + if port in exclude_ports: + continue + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + result = sock.connect_ex(('localhost', port)) + if result != 0: + found_port = port + if found_port is not None: + break + if found_port is None: + return None + os.environ['PYPE_STATICS_SERVER'] = 'http://localhost:{}'.format(found_port) + return found_port