From 1ea0c35f2276197e271a5c354435960202450925 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sat, 7 Jan 2023 16:16:00 +0200 Subject: [PATCH] Add Lesson 6 Task 2 --- Lesson_6/Task 2/.gitignore | 1 + Lesson_6/Task 2/databaseWorker.py | 50 ++++++++++++++++++++++++++++ Lesson_6/Task 2/templates/index.html | 24 +++++++++++++ Lesson_6/Task 2/webserver.py | 28 ++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 Lesson_6/Task 2/.gitignore create mode 100644 Lesson_6/Task 2/databaseWorker.py create mode 100644 Lesson_6/Task 2/templates/index.html create mode 100644 Lesson_6/Task 2/webserver.py diff --git a/Lesson_6/Task 2/.gitignore b/Lesson_6/Task 2/.gitignore new file mode 100644 index 0000000..1e4c958 --- /dev/null +++ b/Lesson_6/Task 2/.gitignore @@ -0,0 +1 @@ +data.db diff --git a/Lesson_6/Task 2/databaseWorker.py b/Lesson_6/Task 2/databaseWorker.py new file mode 100644 index 0000000..3bd9000 --- /dev/null +++ b/Lesson_6/Task 2/databaseWorker.py @@ -0,0 +1,50 @@ +import sqlite3 +from sqlite3 import Error + +def init_conn(path): + conn = None + try: + conn = sqlite3.connect(path) + print ("Connection established!") + except Error as e: + print (e) + print ("Connection failed!") + return conn + +def init_tables(connection): + sql = "CREATE TABLE IF NOT EXISTS hardware( id integer PRIMARY KEY, unit text NOT NULL, value text NOT NULL);" + connection.execute(sql) + +def prepareDb(name): + conn = init_conn(name) + init_tables(conn) + conn.close() + +def getData(name): + connection = init_conn(name) + sql = "SELECT * FROM hardware;" + cursor = connection.cursor() + cursor.execute(sql) + rows = cursor.fetchall() + connection.close() + + return rows + +def generateDataHTMLTable(rows): + dataTable = "" + for row in rows: + dataTable += "" + for cell in row: + dataTable += "" + dataTable += "" + dataTable += "
" + str(cell) + "
" + return dataTable + +def sendData(db, unit, value): + connection = init_conn(db) + sql = "INSERT INTO hardware(`unit`, `value`) VALUES(?, ?);" + args = (unit, value) + cursor = connection.cursor() + cursor.execute(sql, args) + connection.commit() + connection.close() diff --git a/Lesson_6/Task 2/templates/index.html b/Lesson_6/Task 2/templates/index.html new file mode 100644 index 0000000..2546d87 --- /dev/null +++ b/Lesson_6/Task 2/templates/index.html @@ -0,0 +1,24 @@ + + + + Computer info + + +
+
+
+
+
+
+
+
+
+ +
+ Collected data: +
+
+ {{data|safe}} +
+ + diff --git a/Lesson_6/Task 2/webserver.py b/Lesson_6/Task 2/webserver.py new file mode 100644 index 0000000..cfad35a --- /dev/null +++ b/Lesson_6/Task 2/webserver.py @@ -0,0 +1,28 @@ +from databaseWorker import * +from flask import Flask, render_template, redirect, request +import platform, psutil, os + +app = Flask("Computer info") +prepareDb("data.db") + +@app.route('/') +def index(): + rows = getData("data.db") + return render_template("index.html", data=generateDataHTMLTable(rows)) + +@app.route('/send') +def send(): + os.remove("data.db") + prepareDb("data.db") + + if request.args.get('cpu'): + sendData("data.db", "CPU", platform.processor()) + if request.args.get('ram'): + sendData("data.db", "RAM", str(psutil.virtual_memory())) + if request.args.get('architecture'): + sendData("data.db", "Architecture", str(platform.architecture())) + if request.args.get('family'): + sendData("data.db", "Family", platform.machine()) + return redirect('/') + +app.run(host='0.0.0.0', port=8081)