How to specify a port when starting a Flask application
Published on Aug. 22, 2023, 12:16 p.m.
To specify a port when starting a Flask application, you can use the run()
method and pass the port
parameter with the desired port number. For example, if you want to start the application on port 5000, you can do the following:
from flask import Flask
app = Flask(__name__)
# Define your routes and views here
if __name__ == '__main__':
app.run(port=5000)
This will start the Flask application on port 5000. You can replace 5000
with any other port number that you prefer.
To specify a port for a Flask application when starting it from the command line
To specify a port for a Flask application when starting it from the command line, you can use the flask run
command and pass the -p
or --port
parameter followed by the desired port number. Here’s an example of how to start a Flask application on port 5000 from the command line:
export FLASK_APP=yourapp.py
flask run --port=5000
Replace “yourapp.py” with the actual filename of your Flask application. This will start the Flask application on port 5000. You can use a different port number if you prefer.