Files
conjurer/communication_subroutine.py
T
2024-04-09 01:15:12 +02:00

66 lines
1.7 KiB
Python

import logging
import threading
import json
from flask import Flask, jsonify, request
from waitress import serve
HOST_ADDRESS = "192.168.1.191"
PORT_ADDRESS = 5000
app = Flask(__name__)
@app.route("/conjurer", methods=["POST"])
def answer_external_command():
record = json.loads(request.data)
app.logger.info(record)
#if "UUID" in record and "QUERY" in record and "AUTHOR" in record:
# answer_data = "Query logged"
#return_data = (
# jsonify(isError=False, message="Success", statusCode=200, data=answer_data),
# 200,
#)
#return return_data
return jsonify("SUCCESS")
def flask_debug(_logger):
"""
The `flask_debug` function starts a Flask application in debug mode without using the reloader.
Do not use for production for fucks sake.
"""
_logger.info("Attempt debug")
# trunk-ignore(bandit/B201)
app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS)
def waitress_run(_logger):
"""
The `waitress_run` function serves the `app` on host "0.0.0.0"
and port 5000 using the Waitress WSGI server.
"""
_logger.info("Attempt waitress")
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
def scan_queue(comm_q):
while True:
data = comm_q.get()
print(data)
def comm_subroutine(comm_q):
queue = comm_q
logger = logging.getLogger('conjurer_musician')
logger.setLevel(logging.DEBUG)
logger.info("Started")
threads = []
threads.append(threading.Thread(target=waitress_run, args=(logger,)))
threads.append(threading.Thread(target=scan_queue, args=(queue,)))
for worker in threads:
worker.start()
for worker in threads:
worker.join()