How to change the Port and Host in a Flask application

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
5 min

banner

# Table of Contents

  1. How to change the Port and Host in a Flask application
  2. Change the Port and Host in a Flask application using the command line
  3. Changing the host and port in a Flask application with environment variables
  4. Starting your Flask application on port 80

# How to change the Port and Host in a Flask application

To change the port and host in a Flask application:

  1. Pass the host and port arguments in the call to the app.run() method.
  2. Start your Flask application with python app.py.

Create an app.py file with the following code.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>" port_number = 7000 if __name__ == '__main__': app.run(debug=True, host='localhost', port=port_number)

Notice that we passed the host and port keyword arguments when calling app.run().

The host is set to localhost and the port is set to 7000 in the example.

By default, the port is set to 5000.

You can now start your Flask application with the python app.py command.

shell
python app.py # Or with python3 python3 app.py

Make sure to replace app.py with the name of your Flask script.

change port and host of flask application

As shown in the screenshot, the development server is started on port 7000.

development server started on port 7000

The if statement that checks if __name__ is equal to '__main__' ensures that the if block only runs if the file is run directly with python app.py.

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

The __name__ global variable is only set to '__main__' when the file is run directly with python app.py and not when it's imported into a different file.

# Change the Port and Host in a Flask application using the command line

You can also change the port and host in a Flask application using the command line.

Suppose you have an app.py file that contains the following code.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>"

You can change the default host and port by running the following command.

shell
flask run -h localhost -p 7000

change default flask host and port using command line

The -h option stands for host and the -p option stands for port.

The example starts your development server on localhost port 7000.

You can also use the more verbose syntax when running the command.

shell
flask run --host localhost --port 7000

change host and port using more verbose syntax

You can run the flask run --help command to see the full list of options.

shell
flask run --help

flask run help command

Note that the flask run command has higher precedence than calling app.run() in your app.py file.

Suppose, you have an app.py file that contains the following code.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>" port_number = 4000 if __name__ == '__main__': app.run(debug=True, host='localhost', port=port_number)

We called app.run() with a port of 4000.

If I now issue the flask run command with a different port, I can see that flask run has higher precedence.

main.py
flask run --host localhost --port 7000

flask run has higher precedence

Notice that the development server is started on port 7000.

app.run() and flask run are not related.

If you want to start your app.py file with the port that is specified in the call to app.run(), you have to start your development server using the python app.py command.

shell
python app.py # or python3 python3 app.py

If you want to solve the warning "This is a development server. Do not use it in a production deployment", you have to use a production-ready server.

I've written a step-by-step guide on setting up a production-ready Flask server.

# Changing the host and port in a Flask application with environment variables

You can also use environment variables to change the host and port in a Flask application.

Suppose you have the following app.py file.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>"

If you are on macOS or Linux, use the export command.

shell
export FLASK_RUN_PORT=7000 export FLASK_RUN_HOST="127.0.0.1" flask run

change host and port using environment variables

If you are on Windows, use the set command to set the environment variables.

cmd
set FLASK_RUN_PORT=7000 set FLASK_RUN_HOST="127.0.0.1" flask run

The examples set the port to 7000 and the host to localhost, make sure to adjust the values as you see fit.

After setting the FLASK_RUN_PORT and FLASK_RUN_HOST environment variables, you can issue the flask run command and it will pick up the specified values.

You can also set your environment variables in a file called .flaskenv.

  1. Create the .flaskenv file in the root directory of your project.
.flaskenv
FLASK_APP = app FLASK_DEBUG = True FLASK_RUN_HOST="127.0.0.1" FLASK_RUN_PORT=7000

create flaskenv file

Make sure to update the values for the host and port if necessary.

  1. Open your terminal in your project's root directory and install the python-dotenv module.
shell
pip install python-dotenv # Or with pip3 pip3 install python-dotenv

Initialize the dotenv module in your app.py file.

app.py
from flask import Flask from dotenv import load_dotenv app = Flask(__name__) # Take environment variables from .flaskenv load_dotenv() @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>"

Now you can start your Flask application with the flask run command.

shell
flask run

issue flask run command

# Starting your Flask application on port 80

If you want to start your Flask application on port 80, you have to set the host to 0.0.0.0 and the port to 80.

Here is an example of doing this in a file called app.py.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>" if __name__ == '__main__': app.run(host='0.0.0.0', port=80)

You can now start the server with python app.py.

shell
python app.py python3 app.py

Alternatively, you can start your Flask application on port 80 using a command.

Suppose you have the following app.py file.

app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>bobbyhadz.com</p>"

Issue the following command to start your Flask application on port 80.

shell
flask run --host=0.0.0.0 --port=80

If you want to set up a production-ready Flask server, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.