'How can I change the host and port that the flask command uses?

I want to change the host and port that my app runs on. I set host and port in app.run, but the flask run command still runs on the default 127.0.0.1:8000. How can I change the host and port that the flask command uses?

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3000)
set FLASK_APP=onlinegame
set FLASK_DEBUG=true
python -m flask run


Solution 1:[1]

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host="localhost", port=8000, debug=True)

Configure host and port like this in the script and run it with

python app.py

Solution 2:[2]

You can also use the environment variable FLASK_RUN_PORT, for instance:

export FLASK_RUN_PORT=8000
flask run
 * Running on http://127.0.0.1:8000/

Source: The Flask docs.

Solution 3:[3]

When you run the application server using the flask run command, the __name__ of the module is not "__main__". So the if block in your code is not executed -- hence the server is not getting bound to 0.0.0.0, as you expect.

For using this command, you can bind a custom host using the --host flag.

flask run --host=0.0.0.0

Source

Solution 4:[4]

You can use this 2 environmental variables:

set FLASK_RUN_HOST=0.0.0.0
set FLASK_RUN_PORT=3000

Solution 5:[5]

You also can use it:

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5002)

and then in the console use it

set FLASK_ENV=development
python app.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Akronix
Solution 3 TeknasVaruas
Solution 4 Knemay
Solution 5 StasNemy