【问题标题】:Run a Flask application with GUNICORN使用 GUNICORN 运行 Flask 应用程序
【发布时间】:2022-01-15 13:45:53
【问题描述】:

我正在尝试使用 gunicorn 运行烧瓶应用程序。我正在了解 gunicorn 的工作原理,这是我不了解的。我有以下代码和我正在运行的函数:

from flask import Flask, request, make_response
app = Flask(__name__)

@app.route('/')
def index():
    content = "Hello World"
    fwd_for = "X-Forwarded-For: {}".format(
        request.headers.get('x-forwarded-for', None)
    )
    real_ip = "X-Real-IP: {}".format(
        request.headers.get('x-real-ip', None)
    )
    fwd_proto = "X-Forwarded-Proto: {}".format(
        request.headers.get('x-forwarded-proto', None)
    )

    output = "\n".join([content, fwd_for, real_ip, fwd_proto])
    response = make_response(output, 200)
    response.headers["Content-Type"] = "text/plain"

    return response

我正在运行以下 gunicorn 命令:gunicorn -w 4 app:index

它启动后,我收到一个Internal Server Error 并出现以下类型错误:

TypeError: index() takes 0 positional arguments but 2 were given

据我所知,我没有传递任何参数或参数,但仍然得到这个。有关如何解决此问题的任何建议都会有所帮助。

【问题讨论】:

  • 我已经想通了。这就是我运行 gunicorn 的方式。

标签: python flask gunicorn


【解决方案1】:

启动命令gunicorn -w 4 app:index 针对app.py 中的index 函数。

您需要将其指向app.py 内的app 对象,所以:

gunicorn -w 4 app:app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2019-10-04
    • 2012-02-15
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多