利用python脚本+flask实现web页面跳转

使用前请确保安装python3,nginx,flask 以及python相关的依赖包
pip install flask pandas pymysql openpyxl

 

 

add.py脚本

from flask import Flask, redirect
import subprocess

app = Flask(__name__)

@app.route('/run-script', methods=['POST'])
def run_script():
subprocess.run(['python3', '/www/wwwroot/dujiaoka/public/insert.py'])
return redirect('/user.html') # 静态页面路径

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

 

nginx配置反向代理

location /run-script {
proxy_pass http://127.0.0.1:5000/run-script;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

 

web前端请求页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>调用 Python 脚本</title>
</head>
<body>
<h1>点击按钮调用 Python 并跳转</h1>
<form action="http://127.0.0.1:5000/run-script" method="post">
<button type="submit">执行并跳转</button>
</form>
</body>
</html>

 

另外必须让python脚本在后台持续运行

root@jgui:/python# nohup python3 add.py > add.log 2>&1 &
[1] 2212336

posted @ 2025-04-09 17:25  一只竹节虫  阅读(45)  评论(0)    收藏  举报