Last updated: Apr 11, 2024
Reading time·5 min
To change the port and host in a Flask application:
host
and port
arguments in the call to the app.run()
method.python app.py
.Create an app.py
file with the following code.
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.
python app.py # Or with python3 python3 app.py
Make sure to replace app.py
with the name of your Flask script.
As shown in the screenshot, the development server is 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
.
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.
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.
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.
flask run -h localhost -p 7000
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.
flask run --host localhost --port 7000
You can run the flask run --help
command to see the full list of options.
flask run --help
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.
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.
flask run --host localhost --port 7000
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.
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.
You can also use environment variables to change the host and port in a Flask application.
Suppose you have the following app.py
file.
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.
export FLASK_RUN_PORT=7000 export FLASK_RUN_HOST="127.0.0.1" flask run
If you are on Windows, use the set
command to set the environment variables.
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
.
.flaskenv
file in the root directory of your project.FLASK_APP = app FLASK_DEBUG = True FLASK_RUN_HOST="127.0.0.1" FLASK_RUN_PORT=7000
Make sure to update the values for the host and port if necessary.
pip install python-dotenv # Or with pip3 pip3 install python-dotenv
Initialize the dotenv
module in your app.py
file.
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.
flask run
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
.
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
.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path