Flask小项目代码!避免跨站,设计路由,登录jenkins,修改服务器文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值