Change Port in Flask

Change Port in Flask

We are about to learn how to change the port when we run our flask app through the command line interface and how to run the flask app on different ports at the same time and in different operating systems.

Change the Default Port in Flask

Mostly the beginners in Flask would use the following code to define a port where they want to run their flask app.

if __name__=='__main__':
    app.run(port=1000)

But instead of doing that, you can also do it on the command-line interface, which is the recommended way of doing it so that we will open a terminal in the directory where the flask app is located and then run the following command if you are working on Windows.

set FLASK_APP=app.py

If you are working on a UNIX-based operating system, you must use the following command.

export FLASK_APP=app.py

Technically we do not need to pass a file name like app.py because it automatically looks for app.py when we do not specify it, but to make it explicit, we added it there.

We will use the below command to run the flask app.

Flask run

When we run it, the default configuration runs on port 5000, and we can go to the browser and check the port and what port is running.

Output:

Run Flask default configuration

Suppose we want to change the port for whatever reason. For example, we have a couple of flask apps, and in that case, we are creating different APIs and testing several APIs simultaneously, but they can not be on the same port, so we would have to change the port for at least one of them.

Let’s stop our server, and there are a couple of ways you can do this configuration variable. We look at the first method to define the port, and we can use the following command in the UNIX-based operating system.

export FLASK_RUN_PORT=8000

One way of doing it is on the Windows operating system.

flask run --port=80

When we run this command, we see in the output below that it runs on port 80. If we change this and it is working, we try to return to port 5000, but it no longer works.

Change port in Flask

You can also use the traditional way to define a specific port, as described in the beginning.

Full Source Code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def PORT_FUNC():
    return '<h2>Hi there, this is port testing<h2/>'

# set FLASK_APP=app.py
# unix command
# export FLASK_APP=app.py

# set FLASK_RUN_PORT=8000

# windows command
# flask run --port=80

# if __name__=='__main__':
#     app.run(port=1000)
Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Flask Port

  • Set Port in Flask