简体   繁体   中英

Run a Flask application with GUNICORN

I'm trying to run a flask application with gunicorn. I'm in the process of understanding how gunicorn works, and this is the thing I am not understanding. I have the following code with a function I am running:

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

I am running the following gunicorn command: gunicorn -w 4 app:index

After it starts, I'm getting an Internal Server Error and am presented with the following TypeError:

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

To my knowledge, I am not passing in any arguments or parameters but am still getting this. Any advice on how I can fix this would be helpful.

The launch command gunicorn -w 4 app:index aims for the index function within app.py .

You need to point it at the app object within app.py so:

gunicorn -w 4 app:app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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