如何更改烧瓶命令使用的主机和端口?

我想改变我的应用程序运行的主机和端口。我在 app.run中设置了 hostport,但是 flask run命令仍然在默认的 127.0.0.1:8000上运行。如何更改 flask命令使用的主机和端口?

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

The flask command is separate from the flask.run method. It doesn't see the app or its configuration. To change the host and port, pass them as options to the command.

flask run -h localhost -p 3000

Pass --help for the full list of options.

Setting the SERVER_NAME config will not affect the command either, as the command can't see the app's config.


Never expose the dev server to the outside (such as binding to 0.0.0.0). Use a production WSGI server such as uWSGI or Gunicorn.

gunicorn -w 2 -b 0.0.0.0:3000 myapp:app

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

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.

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

You also can use it:

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

and then in the terminal run this

set FLASK_ENV=development
python app.py

You can use this 2 environmental variables:

set FLASK_RUN_HOST=0.0.0.0
set FLASK_RUN_PORT=3000