From 2a829396c2f2bfbbe5f6a9e1706e5354f2021638 Mon Sep 17 00:00:00 2001 From: Mateusz779 Date: Fri, 14 Apr 2023 12:37:37 +0200 Subject: [PATCH] code cleanup --- app.py | 43 ++++++------------------------------------- config.py | 3 ++- utils.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/app.py b/app.py index afa47bc..ccda6d7 100644 --- a/app.py +++ b/app.py @@ -5,49 +5,15 @@ import db import os from werkzeug.utils import secure_filename import subprocess -import threading import utils import shutil +import config app = Flask(__name__) app.config['UPLOAD_FOLDER'] = "squash" app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 #512MB -def ssh_thread_function(): - subprocess.run(['wssh','--fbidhttp=False']) - -ssh_thread = threading.Thread(target=ssh_thread_function) -ssh_thread.start() - -class PingThread(threading.Thread): - def __init__(self, ip, id): - super(PingThread, self).__init__() - self.Ip = ip - self.Id = id - def run(self): - if utils.ping_client(self.Ip) == False: - date = db.get_image_allocation_time_id(self.Id) - if date is None: - return - delta = datetime.datetime.now() - date - if delta.total_seconds() > 30: - db.del_image_allocation_id(self.Id) - else: - db.update_image_allocation_time(self.Id) - -def check_allocation_thread_function(): - while True: - ids = db.get_image_allocation_all() - for x in ids: - ip = db.get_image_allocation_clientip_id(x[0]) - ping_thread = PingThread(ip, x[0]) - ping_thread.start() - - sleep(10) - -allocation_thread = threading.Thread(target=check_allocation_thread_function) -allocation_thread.start() - +utils.init_threads() @app.route('/') def main(): @@ -184,6 +150,9 @@ def get_image(): pass if filename is None or filename == "": - filename = "default.squashfs" + filename = config.default_file return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + +if __name__ == '__main__': + app.run(host="0.0.0.0") \ No newline at end of file diff --git a/config.py b/config.py index 2038351..82d0bff 100644 --- a/config.py +++ b/config.py @@ -2,4 +2,5 @@ database="praktyki" host="localhost" user="praktyki" password="2a7driUITXFy73tO" -port="5432" \ No newline at end of file +port="5432" +default_file = "default.squashfs" \ No newline at end of file diff --git a/utils.py b/utils.py index f97f6e3..35be998 100644 --- a/utils.py +++ b/utils.py @@ -1,8 +1,13 @@ +import datetime import hashlib import secrets import string import random import os +import subprocess +import threading +from time import sleep +import db def generate_random_string(length): letters = string.ascii_letters @@ -21,4 +26,44 @@ def ping_client(ip): if response == 0: return True else: - return False \ No newline at end of file + return False + + + +def ssh_thread_function(): + subprocess.run(['wssh','--fbidhttp=False']) + +def check_allocation_thread_function(): + while True: + ids = db.get_image_allocation_all() + for x in ids: + ip = db.get_image_allocation_clientip_id(x[0]) + ping_thread = PingThread(ip, x[0]) + ping_thread.start() + + sleep(10) + +class PingThread(threading.Thread): + def __init__(self, ip, id): + super(PingThread, self).__init__() + self.Ip = ip + self.Id = id + def run(self): + if self.Ip is None: + return + if ping_client(self.Ip) == False: + date = db.get_image_allocation_time_id(self.Id) + if date is None: + return + delta = datetime.datetime.now() - date + if delta.total_seconds() > 30: + db.del_image_allocation_id(self.Id) + else: + db.update_image_allocation_time(self.Id) + +def init_threads(): + ssh_thread = threading.Thread(target=ssh_thread_function) + ssh_thread.start() + + allocation_thread = threading.Thread(target=check_allocation_thread_function) + allocation_thread.start() \ No newline at end of file