Flask小專案程式碼!避免跨站,設計路由,登入jenkins,修改伺服器檔案
阿新 • • 發佈:2018-11-14
import os import subprocess import re from flask import Flask, render_template, request, jsonify app = Flask(__name__) @app.route('/') @app.route('/index.html') def index(): return render_template('index.html')#html作為response應答顯示! @app.route('/addnumber/',methods=['POST','GET']) #兩種請求方式,一般為get def addnumber (): curdir = os.getcwd() os.chdir('templates') os.system('python3 client.py') os.chdir(curdir) return jsonify("ok") #通過開發者工具可以看到具體返回資訊 #執行jenkins編譯 @app.route('/compile/') def compile(): curdir = os.getcwd() os.chdir('templates') os.system('python3 compile.py') os.chdir(curdir) job_name = request.args.get('b') print(job_name) return jsonify("ok") #將已經連線的埠value值置為1(本地檔案中) @app.route('/portupdate/', methods=['POST', 'GET']) def portuodata(): tempList = [] system = request.args.get('a') value = request.args.get('b') curdir = os.getcwd() os.chdir('templates') print(system) print(value) try: with open('.sys.temp', 'r') as fd, \ open('1.txt', 'w') as f: for line in fd.readlines(): print(fd.readlines()) print(line) print(re.search(r'<option value="\d">(?P<sys>.+):(?P<ip>.+):(?P<port>.+)</option>$', line).group("sys")) if re.search(r'<option value="\d">(?P<sys>.+):(?P<ip>.+):(?P<port>.+)</option>$', line).group("sys") == system: line = line.replace('="0"', '="1"') print(line) f.write(line) tempList.append(line) else: tempList.append(line) f.write(line) except Exception as e: print(e) print(tempList) os.remove('.sys.temp') os.rename('1.txt', '.sys.temp') os.chdir(curdir) return jsonify("ok") #將所有關閉後的埠value置為0(一般是連線後進行互動後的)(本地檔案中) @app.route('/goback/', methods=['POST', 'GET']) def goback(): value = request.args.get('a', 0, type=float) system = request.args.get('b') curdir = os.getcwd() os.chdir('templates') try: with open('.sys.temp', 'r') as fd, \ open('1.txt', 'w') as f: for line in fd.readlines(): print(line) print(re.search(r'<option value="\d">(?P<sys>.+):(?P<ip>.+):(?P<port>.+)</option>$', line).group("sys")) if re.search(r'<option value="\d">(?P<sys>.+):(?P<ip>.+):(?P<port>.+)</option>$', line).group("sys") == system: line.replace('="1"', '="0"') print(line) f.write(line) tempList.append(line) tempList[-1].replace('="1"', '="0"') else: tempList.append(line) f.write(line) except Exception as e: print(e) print(tempList) os.remove('.sys.temp') os.rename('1.txt', '.sys.temp') os.chdir(curdir) return jsonify("ok") @app.route('/<uname>') def uf(uname): return render_template(uname) #引數傳遞 if __name__ == "__main__": app.run(host='0.0.0.0', debug=False) #關閉除錯模式,避免洩露資訊
其中1和0的檔案替換是為了select二級聯動以後,點選過的case便不再顯示!可惜效果失敗,因為快取!