close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I change the host and port that the flask command use in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    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.

  2. change the host and port that the flask command uses

    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.

Method 1

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

Method 2

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

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read