【问题标题】:Nginx blocks data from HTML FormNginx 阻止来自 HTML 表单的数据
【发布时间】:2021-07-07 10:13:15
【问题描述】:

我有 Flask 应用程序,它使用 HTML 表单从用户那里获取数据,通过 bash 脚本运行它并通过另一个 HTML 将其返回给用户。如果我通过命令行python3 app.py 运行它,一切正常,但是当它由 NGINX 处理时,request.form.get('email') 中没有数据。

应用程序.py

from flask import Flask, render_template, request
import subprocess
app = Flask(__name__)
app._static_folder = "/home/my_site/templates/static"

@app.route('/', methods = ['GET'])
def show_index_html():
    return render_template('index.html')

@app.route('/send_data', methods = ['POST'])
def get_email_from_html():
    email = request.form.get('email') ###No data here
    output = subprocess.run(['/home/path_to_script/query.sh', email], capture_output=True)
    score = output.stdout
    return render_template('output.html', output=score)


if (__name__ == '__main__'):
    app.run(ssl_context=('mailcheck.crt','mailcheck.key'),host="0.0.0.0", port=8888,debug=True)

索引.html

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="utf-8>
      <!--<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">-->
    <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='main.css')}}">
</head>
<body>
<div class="box"> 
    <form method="POST" action="/send_data">
        <div class = "form-group">
            <span>E-mail :</span>
            <input type="email" class="email_form" name="email" maxlength="30">
        </div>
        <input type="submit" class="button" value="Send">
    </form>
</div> 
</body>
</html>

网站.ini

[uwsgi]
module = wsgi:app
master=true
processes = 5

socket = site.sock
chmod-socket = 666
vacuum = true

die-on-term = true

Nginx 服务器块

server {
    listen 443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    server_name mailcheck.mysite.com www.mailcheck.mysite.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/soc/site/site.sock;
    }
}

server {
    listen 80;

    server_name mailcheck.mysite.com www.mailcheck.mysite.com;
    return 301 https://$server_name$request_uri;
}

【问题讨论】:

    标签: python nginx flask


    【解决方案1】:

    要从表单中获取数据,您可以使用以下任一方式:

    request.form['input_name']
    

    request.form.get('input_name', 0)
    

    【讨论】:

    • 原来我用的是request.form['input_name'],后来改成request.form.get('input_name', 0)看看有没有区别,可惜还是一样
    • 尝试通过 nginx 应用设置 ssl 证书,而不是通过 Flask 应用。
    • 感谢您的提示,但不幸的是,唯一改变的是当我通过 python 运行应用程序时,它是 HTTP 而不是 HTTPS
    • 它可能是flask默认运行的端口,我没有使用ngnx来运行flask应用程序,尽管它带有自己的网络库。您如何调试它,看看您是否可以点击返回类似 hello 的空端点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多