如何从HTML表单按钮在服务器上执行Python代码

2024-09-28 05:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在用户单击HTML表单按钮时执行python代码。怎么可能呢?用户是否可能无法在服务器上查看python代码?表单输入将成为python代码中的变量。我使用的是Flask框架和python2.7。安全还不是个问题。在

我的路径.py文件:

from pytube import YouTube
from moviepy.editor import *
import os

app = Flask(__name__)

@app.route("/",methods=['GET','POST'])
def landing():
    return render_template("index.html",ytdir=ytdir,ytlink=ytlink)

if __name__ == "__main__":
    app.run(debug=True)

在索引.html在模板文件夹中:

^{pr2}$

基本上,我有一个python代码,通过使用ytdirytlink将YouTube视频下载到本地驱动器中。在

我应该将python代码放在哪里,以便在单击按钮时执行它。在


Tags: 代码用户namefromimport服务器appflask
1条回答
网友
1楼 · 发布于 2024-09-28 05:25:44

我建议你做一个“api调用”

@app.route("/ytdl", methods=["GET"])
def download_video(link):
 # call your download code here 
 # and return the video as an input stream
 # read the flask tutorial on how to do that

在javascript中,使用axios或其他东西使用jquery下载文件 像$button.click(() => axios.get(http://<localhost url>:<port>/ytdl)再去读一遍文档如何做ajax调用

相关问题 更多 >

    热门问题