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/Server/Server.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Web/Server/Server.py (limited to 'Web/Server/Server.py') 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 -- cgit v1.2.3 From 20f9ea9141f77b3c21232efc30202408d535de18 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 13:45:20 -0600 Subject: working web stuff with running page --- Web/PI/PI.py | 25 +++++++++++++---------- Web/PI/Results/results.txt | 2 -- Web/PI/Uploads/upload.py | 13 ------------ Web/PI/test2.py | 6 +++++- Web/Server/Results/results.txt | 2 +- Web/Server/Server.py | 30 ++++++++++++++++------------ Web/Server/static/Server.js | 5 +++++ Web/Server/templates/index.html | 43 ++++++++++++++++++++++++++++++++++++++++ Web/Server/templates/upload.html | 37 ---------------------------------- Web/test.py | 8 ++++++-- 10 files changed, 91 insertions(+), 80 deletions(-) delete mode 100644 Web/PI/Results/results.txt delete mode 100644 Web/PI/Uploads/upload.py create mode 100644 Web/Server/templates/index.html delete mode 100644 Web/Server/templates/upload.html (limited to 'Web/Server/Server.py') diff --git a/Web/PI/PI.py b/Web/PI/PI.py index 5e75298..b607f76 100644 --- a/Web/PI/PI.py +++ b/Web/PI/PI.py @@ -4,9 +4,9 @@ import subprocess import os app = Flask(__name__) -UPLOAD_DESTINATION = "Uploads/upload.py" +UPLOAD_DESTINATION = "Uploads/" -RESULTS_DESTINATION = "Results/results.txt" +RESULTS_DESTINATION = "Results/" @app.route('/') def home(): @@ -18,25 +18,28 @@ def my_test_endpoint(): 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") + 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]) + subprocess.call(["python", UPLOAD_DESTINATION + filename]) # Get results file - with open(RESULTS_DESTINATION, 'r') as results: + results_filename = filename.split(".")[0] + results_filename = results_filename + "_results" + with open(RESULTS_DESTINATION + results_filename, 'r') as results: results_content = results.read() results.close() - - # subprocess.check_output(["echo", "Hello World!"]) - # os.remove(UPLOAD_DESTINATION) - # os.remove(RESULTS_DESTINATION) + + # 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_content':results_content} + dictToReturn = {'results_filename':results_filename, 'results_content':results_content} return jsonify(dictToReturn) if __name__ == '__main__': diff --git a/Web/PI/Results/results.txt b/Web/PI/Results/results.txt deleted file mode 100644 index ce8f671..0000000 --- a/Web/PI/Results/results.txt +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 9cbc73a..0000000 --- a/Web/PI/Uploads/upload.py +++ /dev/null @@ -1,13 +0,0 @@ -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 index 40bbe9c..a5eb806 100644 --- a/Web/PI/test2.py +++ b/Web/PI/test2.py @@ -1,3 +1,7 @@ import subprocess +import os + subprocess.call(["python", "Uploads/upload.py"]) -print("end of script") \ No newline at end of file +print("end of script") + +os.remove("Uploads/upload.py") \ No newline at end of file diff --git a/Web/Server/Results/results.txt b/Web/Server/Results/results.txt index ce8f671..f668b82 100644 --- a/Web/Server/Results/results.txt +++ b/Web/Server/Results/results.txt @@ -1,2 +1,2 @@ THIS IS RESULTS TEXT -Current Time = 21:07:34 \ No newline at end of file +Current Time = 13:41:59 \ No newline at end of file diff --git a/Web/Server/Server.py b/Web/Server/Server.py index bab440a..0c3013b 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -9,10 +9,10 @@ import requests import json app = Flask(__name__) -app.secret_key = "secret key" +app.secret_key = "ski u mah" app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 -ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'py']) +ALLOWED_EXTENSIONS = set(['py']) PI_URL = 'http://localhost:8000' @@ -22,8 +22,8 @@ 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') +def home(): + return render_template('index.html') @app.route('/upload', methods=['POST']) def upload_file(): @@ -41,30 +41,34 @@ def upload_file(): # Send the file content as a post to the PI if file and allowed_file(file.filename): - dictToSend = {'file_content':file.read()} + dictToSend = {'filename':file.filename, '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_filename = json.loads(response.text)[u'results_filename'] .encode("ascii") + results_content = json.loads(response.text)[u'results_content'].encode("ascii") + flash('Results file:' + results_filename) + flash('Response from server:' + results_content) + results = open(RESULTS_DESTINATION, "w") - results.write(contents) + results.write(results_content) results.close() - return render_template('upload.html', results = 'True') + return render_template('index.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 +@app.route('/results', methods=['GET']) def download(): - print("in downlaods") + # Grab content from results file with open(RESULTS_DESTINATION, 'r') as results: results_content = results.read() results.close() - print("results_content") + + # Put content as a download file return Response( results_content, mimetype="text/csv", diff --git a/Web/Server/static/Server.js b/Web/Server/static/Server.js index 49f1f13..30274f2 100644 --- a/Web/Server/static/Server.js +++ b/Web/Server/static/Server.js @@ -2,4 +2,9 @@ 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 @@ + + + + + + + ANDI'S PIE +
+

Select a file to upload

+

+ {% with messages = get_flashed_messages() %} + {% if messages %} +

    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} +

+ +
+
+

+ +

+
+

+ +

+
+
+ +
+ + {% if results %} +
+ +
+ {% endif %} + + + \ No newline at end of file diff --git a/Web/Server/templates/upload.html b/Web/Server/templates/upload.html deleted file mode 100644 index 38c4a09..0000000 --- a/Web/Server/templates/upload.html +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - Python Flask File Upload Example -

Select a file to upload

-

- {% with messages = get_flashed_messages() %} - {% if messages %} -

- {% endif %} - {% endwith %} -

-
-
-

- -

-
-

- -

-
- - {% if results %} -
- -
- {% endif %} - - \ No newline at end of file diff --git a/Web/test.py b/Web/test.py index c6f871c..634bc84 100644 --- a/Web/test.py +++ b/Web/test.py @@ -1,11 +1,15 @@ -import time, os +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/results.txt", "w+") +f = open("Results/" + file_name + "_results", "w+") f.write("THIS IS RESULTS TEXT\n") f.write("Current Time = " + current_time) f.close() -- cgit v1.2.3 From 97fa715c87c72541c83e463d8c1e82315fb471a9 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 14:07:10 -0600 Subject: fixed space --- Web/Server/Server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Web/Server/Server.py') diff --git a/Web/Server/Server.py b/Web/Server/Server.py index 0c3013b..f61419a 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -41,13 +41,14 @@ def upload_file(): # 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') flash('Running test') response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend) - results_filename = json.loads(response.text)[u'results_filename'] .encode("ascii") + results_filename = json.loads(response.text)[u'results_filename'].encode("ascii") results_content = json.loads(response.text)[u'results_content'].encode("ascii") flash('Results file:' + results_filename) flash('Response from server:' + results_content) @@ -58,7 +59,7 @@ def upload_file(): return render_template('index.html', results = 'True') else: - flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') + flash('Allowed file types are .py') return redirect(request.url) @app.route('/results', methods=['GET']) -- cgit v1.2.3 From 7a9ac11fdfebe5c6d39d46282eecc3d62d5885d5 Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 14:10:06 -0600 Subject: removed unnecessary --- Web/Server/Server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Web/Server/Server.py') diff --git a/Web/Server/Server.py b/Web/Server/Server.py index f61419a..45f06b0 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -48,8 +48,8 @@ def upload_file(): flash('Running test') response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend) - results_filename = json.loads(response.text)[u'results_filename'].encode("ascii") - results_content = json.loads(response.text)[u'results_content'].encode("ascii") + 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) -- cgit v1.2.3 From d126a9ea02c1edceff30efa6fe0e061c2c19062a Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 14:16:52 -0600 Subject: tabs to spaces smh --- Web/PI/Uploads/test.py | 17 ++++++++++ Web/Server/Server.py | 84 +++++++++++++++++++++++++------------------------- 2 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 Web/PI/Uploads/test.py (limited to 'Web/Server/Server.py') 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/Server/Server.py b/Web/Server/Server.py index 45f06b0..ce695fb 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -19,62 +19,62 @@ 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 - + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + @app.route('/') def home(): - return render_template('index.html') + return render_template('index.html') @app.route('/upload', methods=['POST']) def upload_file(): - if request.method == 'POST': + 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) + 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) + # 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): + # 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') - flash('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() + dictToSend = {'filename':file.filename, 'file_content':file.read()} + file.close + print('Running test') + flash('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) + 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() + # 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"}) + # 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 -- cgit v1.2.3 From 740c3f998279d4ae74b13f03f95d2b5ee215e06c Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 15:38:00 -0600 Subject: Update Server.py --- Web/Server/Server.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Web/Server/Server.py') diff --git a/Web/Server/Server.py b/Web/Server/Server.py index ce695fb..3e93d00 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -16,6 +16,9 @@ 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.txt" def allowed_file(filename): @@ -45,7 +48,6 @@ def upload_file(): dictToSend = {'filename':file.filename, 'file_content':file.read()} file.close print('Running test') - flash('Running test') response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend) results_filename = json.loads(response.text)[u'results_filename'] -- cgit v1.2.3 From 6e4a89f2fa5adb3137b4f9abdfddc2d6301b936e Mon Sep 17 00:00:00 2001 From: Dat Nguyen Date: Thu, 21 Nov 2019 15:41:40 -0600 Subject: changed to .csv --- Web/PI/PI.py | 4 +++- Web/Server/Server.py | 2 +- Web/test.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'Web/Server/Server.py') diff --git a/Web/PI/PI.py b/Web/PI/PI.py index b607f76..a6a597b 100644 --- a/Web/PI/PI.py +++ b/Web/PI/PI.py @@ -29,7 +29,7 @@ def my_test_endpoint(): # Get results file results_filename = filename.split(".")[0] - results_filename = results_filename + "_results" + results_filename = results_filename + "_results.csv" with open(RESULTS_DESTINATION + results_filename, 'r') as results: results_content = results.read() results.close() @@ -44,3 +44,5 @@ def my_test_endpoint(): 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/Server/Server.py b/Web/Server/Server.py index 3e93d00..a936b96 100644 --- a/Web/Server/Server.py +++ b/Web/Server/Server.py @@ -19,7 +19,7 @@ PI_URL = 'http://localhost:8000' # UNCOMMENT IF RUNNING PI.PY ON PI #PI_URL = 'http://192.168.1.10:8000' -RESULTS_DESTINATION = "Results/results.txt" +RESULTS_DESTINATION = "Results/results.csv" def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS diff --git a/Web/test.py b/Web/test.py index 634bc84..8c15f58 100644 --- a/Web/test.py +++ b/Web/test.py @@ -9,7 +9,7 @@ now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time = " + current_time) -f = open("Results/" + file_name + "_results", "w+") +f = open("Results/" + file_name + "_results.csv", "w+") f.write("THIS IS RESULTS TEXT\n") f.write("Current Time = " + current_time) f.close() -- cgit v1.2.3