from flask import Flask, render_template, request from PIL import Image import pyzbar.pyzbar as pyzbar import time import sqlite3 import os SQLITE_DB = "qr_results1.db3" FLUSH_TABLE = True app = Flask(__name__) global sDB class Sqlite_db: def __init__(self) -> None: self.conn = conn = sqlite3.connect(SQLITE_DB, check_same_thread=False) self.cur = conn.cursor() def create_table(self, table_name="QR_result"): self.cur.execute(f"DROP TABLE IF EXISTS {table_name}") self.cur.execute(f'''create table {table_name}(\ id INTEGER PRIMARY KEY AUTOINCREMENT,\ content varchar(100) not null,\ parse_time varchar(100) not null);''') self.conn.commit() print(f"[*]create table {table_name}") def update_data(self, query): self.cur.execute(query) self.conn.commit() print(f"[*]executed: {query}") def query_data(self, query): self.cur.execute(query) return self.cur.fetchall() def exit(self): self.conn.close() @app.route('/') def hello_world(): return 'Hello World' @app.route('/qr', methods=['POST']) def parse_qr(): file = request.files['qr_pic']#file.filename img = Image.open(file) barcodes = pyzbar.decode(img) if len(barcodes): result = barcodes[0].data.decode("utf-8") else: result = "Null" cur_time = time.strftime("%Y-%m-%d %H-%M:%S", time.localtime(time.time())) query = f"insert into QR_result (content, parse_time) values ('{result}', '{cur_time}')" sDB.update_data(query) return result @app.route("/match", methods=['POST']) def list_results(): pattern = request.form.get('pattern') query = f"select content, parse_time from QR_result where content like '%{pattern}%'" results = sDB.query_data(query) page_content ="" for r in results: page_content += f"" page_content += "
QR ContentParse Time
{r[0]}{r[1]}
" return page_content if __name__ == '__main__': sDB = Sqlite_db() if os.path.isfile(SQLITE_DB) and FLUSH_TABLE: sDB.create_table() # app.config['DEBUG'] = True app.run() sDB.exit()