diff options
Diffstat (limited to '')
-rw-r--r-- | Web/PI/PI.py | 39 | ||||
-rw-r--r-- | Web/PI/Results/results.txt | 2 | ||||
-rw-r--r-- | Web/PI/Uploads/upload.py | 13 | ||||
-rw-r--r-- | Web/PI/test2.py | 3 | ||||
-rw-r--r-- | Web/Server/Results/results.txt | 2 | ||||
-rw-r--r-- | Web/Server/Server.py | 75 | ||||
-rw-r--r-- | Web/Server/static/Server.css | 0 | ||||
-rw-r--r-- | Web/Server/static/Server.js | 5 | ||||
-rw-r--r-- | Web/Server/templates/upload.html | 37 | ||||
-rw-r--r-- | Web/test.py | 13 |
10 files changed, 189 insertions, 0 deletions
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 diff --git a/Web/Server/Results/results.txt b/Web/Server/Results/results.txt new file mode 100644 index 0000000..ce8f671 --- /dev/null +++ b/Web/Server/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/Server/Server.py b/Web/Server/Server.py new file mode 100644 index 0000000..bab440a --- /dev/null +++ b/Web/Server/Server.py @@ -0,0 +1,75 @@ +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 = "secret key" +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 + +ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'py']) + +PI_URL = 'http://localhost:8000' + +RESULTS_DESTINATION = "Results/results.txt" + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +@app.route('/') +def upload_form(): + return render_template('upload.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 = {'file_content':file.read()} + file.close + print('Running test') + flash('Running test') + response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend) + contents = json.loads(response.text)[u'results_content'] + flash('Response from server:' + contents) + + results = open(RESULTS_DESTINATION, "w") + results.write(contents) + results.close() + + return render_template('upload.html', results = 'True') + else: + flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') + return redirect(request.url) + +@app.route('/results', methods=['GET'])# this is a job for GET, not POST +def download(): + print("in downlaods") + with open(RESULTS_DESTINATION, 'r') as results: + results_content = results.read() + results.close() + print("results_content") + 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..49f1f13 --- /dev/null +++ b/Web/Server/static/Server.js @@ -0,0 +1,5 @@ +function showLoad(){ + var paragraph = document.getElementById("message"); + var text = document.createTextNode("RUNNING TEST"); + paragraph.appendChild(text); +}
\ No newline at end of file diff --git a/Web/Server/templates/upload.html b/Web/Server/templates/upload.html new file mode 100644 index 0000000..38c4a09 --- /dev/null +++ b/Web/Server/templates/upload.html @@ -0,0 +1,37 @@ +<html> + <head> + <link rel="stylesheet" href="/static/Server.css"> + <script type="text/javascript" src="/static/Server.js"></script> + </head> + <body> + <title>Python Flask File Upload Example</title> + <h2>Select a file to upload</h2> + <p id = "message"> + {% 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> + + {% 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/test.py b/Web/test.py new file mode 100644 index 0000000..c6f871c --- /dev/null +++ b/Web/test.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) |