python-2.7 - Gunicorn 500'ing 在 POST 到 Flask 应用程序上

标签 python-2.7 flask virtualenv gunicorn

这是 Flask 应用程序:

from flask import Flask, request, render_template, redirect, url_for, flash
app = Flask(__name__)

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

@app.route('/contact', methods = ['POST'])
def contact():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        message = request.form['message']

        flash(name)
        flash(email)
        flash(message)

    return redirect(url_for('index'))

if __name__ == '__main__':
    app.debug = True
    app.secret_key='a;sldfjka;oeiga;lbneas; biaweag'
    app.run()

这是我正在使用的模板:

<!DOCTYPE html>
<html>
    <head>
    </head>

    <body> 
        <form action="/contact" method="post">
            <input type="text" name="name" />
            <input type="text" name="email" />
            <textarea name="message"> </textarea>
            <input type="submit" />
        </form> 
        <ul>
        {% for message in get_flashed_messages() %}
            <li><h3> {{ message }} </h3></li>
        {% endfor %}
        </ul>
    </body>
</html>

这是我用来提供服务的 gunicorn 命令行:

gunicorn --error-logfile err.log --access-logfile access.log \
--log-level debug -b 127.0.0.1:5000 --debug -w 4 wsgi:app

它很好地服务于模板的 GET 请求。然而,当我实际 POST 表单时,它已经 500 了。这是响应 header :

HTTP/1.1 500 INTERNAL SERVER ERROR
Server: gunicorn/0.17.2
Date: Thu, 21 Mar 2013 21:57:25 GMT
Connection: close
Content-Type: text/html
Content-Length: 291

以下是gunicorn 日志中显示的内容:

==> err.log <==
2013-03-21 17:12:38 [10092] [DEBUG] POST /contact

==> access.log <==
"127.0.0.1 - - [21/Mar/2013:17:12:38] "POST /contact HTTP/1.1" 500 291 \
"http://localhost:5000/" "Mozilla/5.0 (X11; Linux x86_64; rv:19.0) \
Gecko/20100101 Firefox/19.0"

当我使用 Flask 的内置开发服务器提供服务时,工作正常。

我的大脑告诉我,这是非常简单的事情,我只是错过了。呃。

最佳答案

我在 name == main block 中定义了 Secret_key。 Gunicorn 感到窒息,因为该应用程序没有所说的 key ,而它需要它来处理表单提交。

if __name__ == '__main__':
    app.secret_key = #...

该行需要...其他任何地方,只要在gunicorn 运行脚本时对其进行评估即可。

app = Flask(__name__)
app.secret_key = #...

我认为值得指出的是,如果我设置了任何像样的日志记录,我就会立即发现这个小错误。

关于python-2.7 - Gunicorn 500'ing 在 POST 到 Flask 应用程序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558276/

相关文章:

django - python运行测试: ImproperlyConfigured

python - 制作私有(private)的、自包含的 python 环境的最佳方法是什么?

python - 如果我分别使用 pip 和 pip3 安装 virtualenv,默认值是多少?

python - wtforms TextField/SearchField,具有 flask 应用程序的自动完成功能(类似于谷歌搜索栏)

python - 我应该为 Flask、uWSGI 和 nginx 提供单独的容器吗?

Python Flask,上传文件 - 引发 BuildError(端点,值,方法)

python - 如何从 bash 脚本停用 virtualenv

python - python 中描述符概念的行为(令人困惑)

Python-Sqlite-OperationalError : near "s": syntax error

python - 字符串 .format() 与 % 使用 Unicode 数据格式化