简体   繁体   English

使用 Python 'flask' 应用程序登录并上传文件

[英]Login and upload file using Python 'flask' app

I Have created a flask app which need to upload files to server and also need to take login detail on same html form .我创建了一个 Flask 应用程序,它需要将文件上传到服务器,并且还需要在同一个 html 表单上获取登录详细信息。

I am able to upload file to destination folder , but credential part is not getting fetched/submit to form and unable to get this in flask app for further processing .我能够将文件上传到目标文件夹,但凭证部分没有被获取/提交到表单并且无法在 Flask 应用程序中获取它以进行进一步处理。

i need to do some operation on this file based on user credentials ( i know credential are plain text )我需要根据用户凭据对该文件进行一些操作(我知道凭据是纯文本)

Html form :- Html 形式:-

<form action="/import" enctype="multipart/form-data" method="post">

  <p>User name <input name="username" type="text" /></p>
<p>User password: <input name="psw" type="password" /></p>

<p><input name="file" type="file" /> <input type="submit" value="Submit" /></p>

</form>

Flask Application code :-烧瓶应用程序代码:-

    from flask import Flask, render_template, request
    from werkzeug import secure_filename
    app = Flask(__name__)

    @app.route('/import')
    def import_form():
       return render_template('import.html')

    @app.route('/import', methods = ['GET', 'POST'])
    def import_form():
       if request.method == 'POST':
          username = request.args.get('username')
          psw = request.args.get('psw')
          psw = str(psw)
          username=str(username)
          f = request.files['file']
          f.save(secure_filename(f.filename))
          fname = f.filename

          cmd = ('sh myscript.sh -u '+username+' -p '+psw+' -src '+fname)  # command to pass parameter as user name password and file name in directory 

          p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
              stdout, stderr = p.communicate()
          return out    
if __name__ == '__main__':
       app.run(debug = True)

output of this come as below ( user name and password value come as blank/none )输出如下(用户名和密码值为空/无)

sh myscript.sh -u none -p none -src xyz.file sh myscript.sh -u none -p none -src xyz.file

I want to get user name password and file upload on same page then process this request under a shell script to return output of it我想在同一页面上获取用户名密码和文件上传,然后在 shell 脚本下处理此请求以返回它的输出

Got solution for this issue by one of my colleagues help我的一位同事帮助解决了这个问题

need to use as below to get argument enter on screen form .需要使用如下来在屏幕表单上输入参数。

username = request.form['username']
psw = reques.form['psw']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM