diff options
-rw-r--r-- | Web/PI/PI.py | 48 | ||||
-rw-r--r-- | Web/PI/Results/PLACEHOLDER.txt | 0 | ||||
-rw-r--r-- | Web/PI/Uploads/PLACEHOLDER | 0 | ||||
-rw-r--r-- | Web/PI/Uploads/test.py | 17 | ||||
-rw-r--r-- | Web/PI/test2.py | 7 | ||||
-rw-r--r-- | Web/Server/Results/PLACEHOLDER | 0 | ||||
-rw-r--r-- | Web/Server/Server.py | 82 | ||||
-rw-r--r-- | Web/Server/static/Server.css | 0 | ||||
-rw-r--r-- | Web/Server/static/Server.js | 10 | ||||
-rw-r--r-- | Web/Server/templates/index.html | 43 | ||||
-rw-r--r-- | Web/requirements.txt | 18 | ||||
-rw-r--r-- | Web/test.py | 17 |
12 files changed, 242 insertions, 0 deletions
diff --git a/Web/PI/PI.py b/Web/PI/PI.py new file mode 100644 index 0000000..a6a597b --- /dev/null +++ b/Web/PI/PI.py @@ -0,0 +1,48 @@ +#handle a POST request +from flask import Flask, render_template, request, url_for, jsonify +import subprocess +import os +app = Flask(__name__) + +UPLOAD_DESTINATION = "Uploads/" + +RESULTS_DESTINATION = "Results/" + +@app.route('/') +def home(): + return "ANDI'S PIE" + +@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 + filename=input_json['filename'].encode("ascii") + file_content=input_json['file_content'].encode("ascii") + upload = open(UPLOAD_DESTINATION + filename, "w") + upload.write(file_content) + upload.close() + + # Run python script + subprocess.call(["python", UPLOAD_DESTINATION + filename]) + + # Get results file + results_filename = filename.split(".")[0] + results_filename = results_filename + "_results.csv" + with open(RESULTS_DESTINATION + results_filename, 'r') as results: + results_content = results.read() + results.close() + + # Remove test file and results file now that were done with them + os.remove(UPLOAD_DESTINATION + filename) + os.remove(RESULTS_DESTINATION + results_filename) + + # Return results file content + dictToReturn = {'results_filename':results_filename, 'results_content':results_content} + return jsonify(dictToReturn) + +if __name__ == '__main__': + app.run(host="localhost", port=8000) + # UNCOMMENT IF RUNNING PI.PI ON PI + #app.run(host="192.168.1.10", port=8000) diff --git a/Web/PI/Results/PLACEHOLDER.txt b/Web/PI/Results/PLACEHOLDER.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Web/PI/Results/PLACEHOLDER.txt diff --git a/Web/PI/Uploads/PLACEHOLDER b/Web/PI/Uploads/PLACEHOLDER new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Web/PI/Uploads/PLACEHOLDER diff --git a/Web/PI/Uploads/test.py b/Web/PI/Uploads/test.py new file mode 100644 index 0000000..5091e89 --- /dev/null +++ b/Web/PI/Uploads/test.py @@ -0,0 +1,17 @@ +import time, os, sys
+from datetime import datetime
+
+file_name = os.path.basename(sys.argv[0])
+
+file_name = file_name.split(".")[0]
+
+now = datetime.now()
+current_time = now.strftime("%H:%M:%S")
+print("Current Time = " + current_time)
+
+f = open("Results/" + file_name + "_results", "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..a5eb806 --- /dev/null +++ b/Web/PI/test2.py @@ -0,0 +1,7 @@ +import subprocess +import os + +subprocess.call(["python", "Uploads/upload.py"]) +print("end of script") + +os.remove("Uploads/upload.py")
\ No newline at end of file diff --git a/Web/Server/Results/PLACEHOLDER b/Web/Server/Results/PLACEHOLDER new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Web/Server/Results/PLACEHOLDER diff --git a/Web/Server/Server.py b/Web/Server/Server.py new file mode 100644 index 0000000..a936b96 --- /dev/null +++ b/Web/Server/Server.py @@ -0,0 +1,82 @@ +import os +try: #python3 + from urllib.request import urlopen +except: #python2 + from urllib2 import urlopen +from flask import Flask, flash, request, redirect, render_template, Response +from werkzeug.utils import secure_filename +import requests +import json + +app = Flask(__name__) +app.secret_key = "ski u mah" +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 + +ALLOWED_EXTENSIONS = set(['py']) + +PI_URL = 'http://localhost:8000' + +# UNCOMMENT IF RUNNING PI.PY ON PI +#PI_URL = 'http://192.168.1.10:8000' + +RESULTS_DESTINATION = "Results/results.csv" + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +@app.route('/') +def home(): + return render_template('index.html') + +@app.route('/upload', methods=['POST']) +def upload_file(): + if request.method == 'POST': + # Check if the post request has the file part + if 'file' not in request.files: + flash('No file part') + return redirect(request.url) + + # Grab the file + file = request.files['file'] + if file.filename == '': + flash('No file selected for uploading') + return redirect(request.url) + + # Send the file content as a post to the PI + if file and allowed_file(file.filename): + + dictToSend = {'filename':file.filename, 'file_content':file.read()} + file.close + print('Running test') + response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend) + + results_filename = json.loads(response.text)[u'results_filename'] + results_content = json.loads(response.text)[u'results_content'] + flash('Results file:' + results_filename) + flash('Response from server:' + results_content) + + results = open(RESULTS_DESTINATION, "w") + results.write(results_content) + results.close() + + return render_template('index.html', results = 'True') + else: + flash('Allowed file types are .py') + return redirect(request.url) + +@app.route('/results', methods=['GET']) +def download(): + # Grab content from results file + with open(RESULTS_DESTINATION, 'r') as results: + results_content = results.read() + results.close() + + # Put content as a download file + return Response( + results_content, + mimetype="text/csv", + headers={"Content-disposition": + "attachment; filename=results.csv"}) + +if __name__ == "__main__": + app.run(host="localhost", port=5000)
\ No newline at end of file diff --git a/Web/Server/static/Server.css b/Web/Server/static/Server.css new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Web/Server/static/Server.css diff --git a/Web/Server/static/Server.js b/Web/Server/static/Server.js new file mode 100644 index 0000000..30274f2 --- /dev/null +++ b/Web/Server/static/Server.js @@ -0,0 +1,10 @@ +function showLoad(){ + var paragraph = document.getElementById("message"); + var text = document.createTextNode("RUNNING TEST"); + paragraph.appendChild(text); + + var div = document.getElementById("upload"); + if (div.style.display !== 'none') { + div.style.display = 'none'; + } +}
\ No newline at end of file diff --git a/Web/Server/templates/index.html b/Web/Server/templates/index.html new file mode 100644 index 0000000..70a282b --- /dev/null +++ b/Web/Server/templates/index.html @@ -0,0 +1,43 @@ +<html> + <head> + <link rel="stylesheet" href="/static/Server.css"> + <script type="text/javascript" src="/static/Server.js"></script> + </head> + <body> + <title>ANDI'S PIE</title> + <div id="upload"> + <h2>Select a file to upload</h2> + <p> + {% with messages = get_flashed_messages() %} + {% if messages %} + <ul class=flashes> + {% for message in messages %} + <li>{{ message }}</li> + {% endfor %} + </ul> + {% endif %} + {% endwith %} + </p> + + <form method="post" action="/upload" enctype="multipart/form-data"> + <dl> + <p> + <input type="file" name="file" autocomplete="off" required> + </p> + </dl> + <p> + <input type="submit" value="Submit" onclick="showLoad()"> + </p> + </form> + </div> + + <div id="message"></div> + + {% if results %} + <form method="get" action="/results"> + <button type="submit">Download!</button> + </form> + {% endif %} + + </body> +</html>
\ No newline at end of file diff --git a/Web/requirements.txt b/Web/requirements.txt new file mode 100644 index 0000000..f0cc7a5 --- /dev/null +++ b/Web/requirements.txt @@ -0,0 +1,18 @@ +certifi==2019.9.11 +chardet==3.0.4 +Click==7.0 +Flask==1.1.1 +Flask-Bootstrap==3.2.0.2 +Flask-WTF==0.14.2 +gunicorn==19.9.0 +idna==2.8 +itsdangerous==1.1.0 +Jinja2==2.10.3 +MarkupSafe==1.1.1 +Pillow==2.5.1 +requests==2.22.0 +simplejson==3.6.0 +urllib3==1.25.6 +virtualenv==16.7.7 +Werkzeug==0.16.0 +WTForms==2.2.1 diff --git a/Web/test.py b/Web/test.py new file mode 100644 index 0000000..8c15f58 --- /dev/null +++ b/Web/test.py @@ -0,0 +1,17 @@ +import time, os, sys +from datetime import datetime + +file_name = os.path.basename(sys.argv[0]) + +file_name = file_name.split(".")[0] + +now = datetime.now() +current_time = now.strftime("%H:%M:%S") +print("Current Time = " + current_time) + +f = open("Results/" + file_name + "_results.csv", "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) |