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便不再显示!可惜效果失败,因为缓存!