From 8887dc5a9803969e23b24eb293711c1e32c374e5 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Wed, 20 Nov 2019 21:10:51 -0600 Subject: Web stuff semi working --- Web/PI/PI.py | 39 +++++++++++++++++++++++++++++++++++++++ Web/PI/Results/results.txt | 2 ++ Web/PI/Uploads/upload.py | 13 +++++++++++++ Web/PI/test2.py | 3 +++ 4 files changed, 57 insertions(+) create mode 100644 Web/PI/PI.py create mode 100644 Web/PI/Results/results.txt create mode 100644 Web/PI/Uploads/upload.py create mode 100644 Web/PI/test2.py (limited to 'Web/PI') diff --git a/Web/PI/PI.py b/Web/PI/PI.py new file mode 100644 index 0000000..ae01bf3 --- /dev/null +++ b/Web/PI/PI.py @@ -0,0 +1,39 @@ +#handle a POST request +from flask import Flask, render_template, request, url_for, jsonify +import subprocess +import os +app = Flask(__name__) + +UPLOAD_DESTINATION = "Uploads/upload.py" + +RESULTS_DESTINATION = "Results/results.txt" + +@app.route('/tests/endpoint', methods=['POST']) +def my_test_endpoint(): + # Receive post + input_json = request.get_json(force=True) + + # Put file content into a file caled upload.py + file_content=input_json['file_content'] + upload = open(UPLOAD_DESTINATION, "w") + upload.write(file_content) + upload.close() + + # Run python script + subprocess.call(["python", UPLOAD_DESTINATION], shell=True) + + # Get results file + with open(RESULTS_DESTINATION, 'r') as results: + results_content = results.read() + results.close() + + # subprocess.check_output(["echo", "Hello World!"]) + # os.remove(UPLOAD_DESTINATION) + # os.remove(RESULTS_DESTINATION) + + # Return results file content + dictToReturn = {'results_content':results_content} + return jsonify(dictToReturn) + +if __name__ == '__main__': + app.run(host="localhost", port=8000) \ No newline at end of file diff --git a/Web/PI/Results/results.txt b/Web/PI/Results/results.txt new file mode 100644 index 0000000..ce8f671 --- /dev/null +++ b/Web/PI/Results/results.txt @@ -0,0 +1,2 @@ +THIS IS RESULTS TEXT +Current Time = 21:07:34 \ No newline at end of file diff --git a/Web/PI/Uploads/upload.py b/Web/PI/Uploads/upload.py new file mode 100644 index 0000000..9cbc73a --- /dev/null +++ b/Web/PI/Uploads/upload.py @@ -0,0 +1,13 @@ +import time, os +from datetime import datetime + +now = datetime.now() +current_time = now.strftime("%H:%M:%S") +print("Current Time = " + current_time) + +f = open("Results/results.txt", "w+") +f.write("THIS IS RESULTS TEXT\n") +f.write("Current Time = " + current_time) +f.close() +print("Running test.py for ~ 5 seconds.") +time.sleep(5) diff --git a/Web/PI/test2.py b/Web/PI/test2.py new file mode 100644 index 0000000..40bbe9c --- /dev/null +++ b/Web/PI/test2.py @@ -0,0 +1,3 @@ +import subprocess +subprocess.call(["python", "Uploads/upload.py"]) +print("end of script") \ No newline at end of file -- cgit v1.2.3