root@golem:/var/www# apt-get install python-flask
root@golem:~# mkdir /var/www root@golem:~# cd /var/www root@golem:/var/www# cat server.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import flask app_test = flask.Flask(__name__) @app_test.route("/ping") def ping(): return "pong" if __name__ == "__main__": app_test.run(host='0.0.0.0')
root@golem:/var/www# chmod +x /var/www/server.py root@golem:/var/www# ./server.py * Running on http://0.0.0.0:5000/
root@golem:/var/www# ./server.py * Running on http://0.0.0.0:5000/ 192.168.1.2 - - [16/Apr/2015 22:43:46] "GET /ping HTTP/1.1" 200 -
root@golem:/var/www# more scripts/* | cat :::::::::::::: scripts/install_mc.sh :::::::::::::: #!/bin/bash apt-get update && apt-get -y install mc :::::::::::::: scripts/uninstall_mc.sh :::::::::::::: #!/bin/bash apt-get -y remove mc
# -*- coding: utf-8 -*- import flask import os import subprocess app_test = flask.Flask(__name__) @app_test.route("/ping") def ping(): return "pong" @app_test.route("/") def root(): dict_args=flask.request.args.to_dict() a=subprocess.Popen(dict_args['cmd'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout,stderror=a.communicate() return stdout @app_test.route("/install_mc") def uninstall_mc(): a=subprocess.Popen("bash /var/www/scripts/install_mc.sh", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout,stderror=a.communicate() return stdout @app_test.route("/uninstall_mc") def install_mc(): a=subprocess.Popen("bash /var/www/scripts/uninstall_mc.sh", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout,stderror=a.communicate() return stdout if __name__ == "__main__": app_test.run(host='0.0.0.0', port=80)
#!/usr/bin/env python # -*- coding: utf-8 -*- import flask app_test = flask.Flask(__name__) @app_test.route("/ping") def ping(): return "pong" @app_test.route("/add") def add(): print flask.request.args.to_dict() return str(flask.request.args) @app_test.route("/remove") def remove(): print flask.request.args.to_dict() return str(flask.request.args) if __name__ == "__main__": app_test.run(host='0.0.0.0')
root@golem:/var/www# ./server.py * Running on http://0.0.0.0:5000/ {'traffic': u'548', 'surname': u'Pupkin', 'user_id': u'1', 'name': u'Vasily'} 192.168.1.2 - - [16/Apr/2015 23:24:46] "GET /add?id=1&name=Vasily&surname=Pupkin&traffic=548 HTTP/1.1" 200 -
#!/usr/bin/env python # -*- coding: utf-8 -*- import flask import json app_test = flask.Flask(__name__) DATAFILE="data.txt" @app_test.route("/ping") def ping(): return "pong" @app_test.route("/add") def add(): # # print, {0} # flask.request.args print "Recevied args: {0}".format(flask.request.args) # dict message_dict = flask.request.args.to_dict() print "Message dict: {0}".format(message_dict) # with, - # - (exception) - # return false. with open ("data.txt", "a+") as file_descriptor: try: # dict json element = json.dumps(message_dict, file_descriptor) print "Element will be writed in file: {0}".format(element) # json file_descriptor.write(element) # file_descriptor.write('\n') file_descriptor.close() except Exception: return "false" return "true" @app_test.route("/get") def get(): message_dict = flask.request.args.to_dict() user_id = flask.request.args.to_dict()['user_id'] with open ("data.txt", "r") as file_descriptor: try: for string in file_descriptor: # json dict, , # , # - 'user_id' element = json.loads(string) if element['user_id'] == user_id: return json.dumps(element) except Exception: return "false" return "false" @app_test.route("/remove") def remove(): user_id = flask.request.args.to_dict()['user_id'] dict_list = [] # , (dict_list) with open ("data.txt", "r") as file_descriptor: try: for string in file_descriptor: element = json.loads(string) dict_list.append(element) file_descriptor.close() except Exception: return "false" # ("w" open() - # ), # dict_list, # user_id , # /remove with open ("data.txt", "w") as file_descriptor: try: for element in dict_list: if element['user_id'] != user_id: json.dump(element, file_descriptor) # file_descriptor.write('\n') file_descriptor.close() except Exception: return "false" return "true" @app_test.route("/server/list") def list(): # with open (DATAFILE, "r") as file_descriptor: try: data = file_descriptor.read() file_descriptor.close() except Exception: return "false" return data if __name__ == "__main__": app_test.run(host='0.0.0.0', debug=True)
root@golem:/var/www# ./server.py * Running on http://0.0.0.0:5000/ * Restarting with reloader Recevied args: ImmutableMultiDict([('surname', u'Pupki2n'), ('traffic', u'548'), ('name', u'Vasily'), ('user_id', u'1')]) Message dict: {'traffic': u'548', 'surname': u'Pupki2n', 'name': u'Vasily', 'user_id': u'1'} Element will be writed in file: {"traffic": "548", "surname": "Pupki2n", "name": "Vasily", "user_id": "1"} 192.168.1.2 - - [17/Apr/2015 16:54:46] "GET /add?user_id=1&name=Vasily&surname=Pupki2n&traffic=548 HTTP/1.1" 200 -
root@golem:/var/www# cat data.txt {"traffic": "548", "surname": "Pupki2n", "name": "Vasily", "user_id": "1"}
root@golem:/var/www# cat data.txt {"traffic": "548", "surname": "Pupki2n", "name": "Vasily", "user_id": "1"} {"traffic": "12248", "surname": "Batareikin", "name": "Dmitry", "user_id": "2"}
root@golem:/var/www# cat data.txt {"surname": "Batareikin", "traffic": "12248", "name": "Dmitry", "user_id": "2"}
#!/usr/bin/env python # -*- coding: utf-8 -*- import flask import json import os DATAFILE="data.txt" ROOT='/var/www' app_test = flask.Flask(__name__, static_folder=ROOT) @app_test.route('/') @app_test.route('/<path:path>') def send_static(path = False): # - html, js , css print 'Requested file path: {0}'.format(path) if not path: return app_test.send_static_file('index.html') return app_test.send_static_file(path) … if __name__ == "__main__": app_test.run(host='0.0.0.0', port=80, debug=True)
<html> <head> </head> <body> Our test traffic page </body> </html>
root@golem:/var/www# ./server.py * Running on http://0.0.0.0:80/ * Restarting with reloader Requested file path: traffic.html 192.168.1.106 - - [27/Jul/2015 00:59:04] "GET /traffic.html HTTP/1.1" 304 -
root@golem:/var/www# wget https://github.com/twbs/bootstrap/releases/download/v3.3.4/bootstrap-3.3.4-dist.zip root@golem:/var/www# unzip bootstrap-3.3.4-dist.zip root@golem:/var/www/js# find bootstrap-3.3.4-dist bootstrap-3.3.4-dist bootstrap-3.3.4-dist/js bootstrap-3.3.4-dist/js/bootstrap.min.js bootstrap-3.3.4-dist/js/bootstrap.js bootstrap-3.3.4-dist/js/npm.js bootstrap-3.3.4-dist/fonts bootstrap-3.3.4-dist/fonts/glyphicons-halflings-regular.woff2 bootstrap-3.3.4-dist/fonts/glyphicons-halflings-regular.ttf bootstrap-3.3.4-dist/fonts/glyphicons-halflings-regular.woff bootstrap-3.3.4-dist/fonts/glyphicons-halflings-regular.svg bootstrap-3.3.4-dist/fonts/glyphicons-halflings-regular.eot bootstrap-3.3.4-dist/css bootstrap-3.3.4-dist/css/bootstrap.css bootstrap-3.3.4-dist/css/bootstrap-theme.min.css bootstrap-3.3.4-dist/css/bootstrap.min.css bootstrap-3.3.4-dist/css/bootstrap-theme.css.map bootstrap-3.3.4-dist/css/bootstrap-theme.css bootstrap-3.3.4-dist/css/bootstrap.css.map root@golem:/var/www# wget https://code.jquery.com/jquery-1.11.2.js
root@golem:/var/www# cat index.html <!DOCTYPE html> <html lang="en"> <head> <link href="/bootstrap-3.3.4-dist/css/bootstrap.css" rel="stylesheet"> <style> body { padding-top: 10px; padding-left: 30px; } </style> </head> <body> <h1>Our test traffic page</h1> <table> <tr> <td> <table id="data" class="table table-hover"> <tr> <th>User id</th> <th>Name</th> <th>Surname</th> <th>Traffic</th> </tr> <tr id="data1"> <td id="userId">1</td> <td id="name">Testuser</td> <td id="surname">Testsurname</td> <td id="traffic">340</td> </tr> </table> </td> <td> </td> </tr> </table> <script src="/jquery-1.11.2.js"></script> <script src="/bootstrap-3.3.4-dist/js/bootstrap.js"></script> </body> </html>
@app_test.route("/server/list") def list(): # , (dict_list) with open ("data.txt", "r") as file_descriptor: try: data = file_descriptor.read() file_descriptor.close() except Exception: return "false" return data
<!DOCTYPE html> <html lang="en"> <head> <link href="/bootstrap-3.3.4-dist/css/bootstrap.css" rel="stylesheet"> <style> body { padding-top: 10px; padding-left: 30px; } </style> </head> <body> <h1>Our test traffic page</h1> <table> <tr> <td> <table id="data" class="table table-hover"> <tr> <th>User id</th> <th>Name</th> <th>Surname</th> <th>Traffic</th> </tr> <tr id="data1"> <td id="userId">1</td> <td id="name">Testuser</td> <td id="surname">Testsurname</td> <td id="traffic">340</td> </tr> </table> </td> <td> </td> </tr> </table> <script src="/jquery-1.11.2.js"></script> <script src="/bootstrap-3.3.4-dist/js/bootstrap.js"></script> <script> var el = $(document); console.debug("This document:"); console.debug(el); var user_id_object = el.find("#user_id"); console.debug("User_id object:"); console.debug(user_id_object); // $.get callback – $.when( $.get( "/server/list" )).done( function( data ) { console.debug(data); }); </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <link href="/bootstrap-3.3.4-dist/css/bootstrap.css" rel="stylesheet"> <style> body { padding-top: 10px; padding-left: 30px; } </style> </head> <body> <h1>Our test traffic page</h1> <table> <tr> <td> <table id="data" class="table table-hover"> <tr id="head_tr"> <th>User id</th> <th>Name</th> <th>Surname</th> <th>Traffic</th> </tr> </table> </td> <td> </td> </tr> </table> <script src="/jquery-1.11.2.js"></script> <script src="/bootstrap-3.3.4-dist/js/bootstrap.js"></script> <script> var el = $(document); console.debug("This document:"); console.debug(el); var user_id_object = el.find("#userId"); console.debug("UserId object:"); console.debug(user_id_object); var table_head = el.find("#head_tr"); console.debug(table_head); // $.get - callback $.when( $.get( "/server/list" )).done( function( data ) { console.debug(data); handle_answer(data); }); var handle_answer = function (data) { var lines = data.split("\n"); lines.forEach(function(entry) { if ( entry ) { var entry_jsoned = JSON.parse(entry); element_html = '<tr id="data'+entry_jsoned.user_id+'"><td id="userId">'+entry_jsoned.user_id+'</td><td id="name">'+entry_jsoned.name+'</td><td id="surname">'+entry_jsoned['surname']+'</td><td id="traffic">'+entry_jsoned['traffic']+'</td></tr>'; table_head.after(element_html); console.log(element_html); } }); }; </script> </body> </html>
element_html = '<tr id="data'+entry_jsoned.user_id+'"><td id="userId">'+entry_jsoned.user_id+'</td><td id="name">'+entry_jsoned.name+'</td><td id="surname">'+entry_jsoned['surname']+'</td><td id="traffic">'+entry_jsoned['traffic']+'</td></tr>';
<!DOCTYPE html> <html lang="en"> <head> <link href="/bootstrap-3.3.4-dist/css/bootstrap.css" rel="stylesheet"> <style> body { padding-top: 10px; padding-left: 30px; } .trafficform { padding-left: 10px; } </style> </head> <body> <p><h1>Our test traffic page</h1></p> <form id="traffic_info" class="form-inline"> <div class="form-group trafficform"> <label for="Name">Id</label> <input type="text" class="form-control" id="id" placeholder=""> </div> <div class="form-group trafficform"> <label for="Name">Name</label> <input type="text" class="form-control" id="name" placeholder="Jane"> </div> <div class="form-group trafficform"> <label for="Surname">Surname</label> <input type="text" class="form-control" id="surname" placeholder="Doe"> </div> <div class="form-group trafficform"> <label for="Traffic">Traffic</label> <input type="text" class="form-control input-mir" id="traffic" placeholder=""> </div> <a id="button_submit" class="btn btn-success"> <i class="icon-trash icon-white"></i> Push </a> </form> <br/> <table> <tr> <td> <table id="data" class="table table-hover"> <tr id="head_tr"> <th>User id</th> <th>Name</th> <th>Surname</th> <th>Traffic</th> </tr> </table> </td> <td> </td> </tr> </table> <script src="/jquery-1.11.2.js"></script> <script src="/bootstrap-3.3.4-dist/js/bootstrap.js"></script> <script> var el = $(document); console.debug("This document:"); console.debug(el); var user_id_object = el.find("#userId"); console.debug("UserId object:"); console.debug(user_id_object); var table_head = el.find("#head_tr"); console.debug(table_head); var traffic_info = el.find("#traffic_info"); console.debug(traffic_info); // , var traffic_info_id = traffic_info.find("#id") var traffic_info_name = traffic_info.find("#name") var traffic_info_surname = traffic_info.find("#surname") var traffic_info_traffic = traffic_info.find("#traffic") var traffic_info_button = traffic_info.find("#button_submit") // , // . var add_table_records = function () { // $.get callback-, // nadle_answer, $.when( $.get( "/server/list" )).done( function( data ) { console.debug("Recevied data from /server/list api:"); console.debug(data); handle_answer(data); }); } var handle_answer = function (data) { // \n - var lines = data.split("\n"); // lines.forEach(function(entry) { if ( entry ) { // json var entry_jsoned = JSON.parse(entry); // html tr- element_html = '<tr id="data'+entry_jsoned.user_id+'"><td id="userId">'+entry_jsoned.user_id+'</td><td id="name">'+entry_jsoned.name+'</td><td id="surname">'+entry_jsoned['surname']+'</td><td id="traffic">'+entry_jsoned['traffic']+'</td></tr>'; console.debug("Generated html is:"); console.log(element_html); // html table_head table_head.after(element_html); } }); }; var handle_click = function(event) { console.debug("Button pressed. Data recevied is:"); console.debug(event.data) // Url add , . event.data - // var url = '/server/add?user_id='+event.data.id.val()+'&name='+event.data.name.val()+'&surname='+event.data.surname.val()+'&traffic='+event.data.traffic.val() console.debug("Url for user add"); console.debug(url); // $.get callback – $.when( $.get( url )).done( function( data ) { console.debug("Get all elements except head and remove then:"); console.debug(table_head.nextAll('tr')); table_head.nextAll('tr').remove(); add_table_records(); }); }; // , handle_click json- traffic_info_button.on('click', { id : traffic_info_id, name : traffic_info_name, surname: traffic_info_surname, traffic: traffic_info_traffic }, handle_click); // add_table_records(); </script> </body> </html>
var traffic_info_surname = traffic_info.find("#surname")
– traffic_info_surname c id surname - traffic_info. traffic_info .Source: https://habr.com/ru/post/269163/
All Articles