Flask is a Python web framework that allows you to quickly build web applications. In your Flask app, you may want to change the default port that Flask uses to run your application. This can be useful if you need to run multiple instances of your Flask app on the same machine, or if you need to access your Flask app from another computer on your network.
In this article, we will show you how to change the default port that Flask uses to run your application with code examples.
Step 1: Import Flask and create an instance of the Flask class
The first step in creating a Flask app is to import the Flask module and create an instance of the Flask class. Here is an example of how to do this:
from flask import Flask
app = Flask(__name__)
This creates a new instance of the Flask class and assigns it to the variable app
.
Step 2: Run the Flask app on the default port
By default, Flask runs on port 5000. To run your Flask app on the default port, all you need to do is call the run()
method on your Flask app instance:
if __name__ == '__main__':
app.run()
This will start the Flask development server and your app will be accessible at http://localhost:5000/
.
Step 3: Change the default port
To change the default port that Flask uses to run your application, you can pass the port
parameter to the run()
method. For example, to run your Flask app on port 8080, you can do the following:
if __name__ == '__main__':
app.run(port=8080)
Now your Flask app will be accessible at http://localhost:8080/
.
Step 4: Use an environment variable to set the port
Instead of hardcoding the port number in your code, you can use an environment variable to set the port number. This makes it easier to change the port number without having to modify your code. Here's an example of how to use an environment variable to set the port number:
import os
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(port=port)
In this example, we're using the os.environ.get()
function to get the value of the PORT
environment variable. If the PORT
environment variable isn't set, we're using the default port number of 5000. By wrapping the os.environ.get()
function with int()
, we ensure that the port variable is an integer.
Now you can set the PORT
environment variable to any value you like, and your Flask app will run on that port. For example, if you want to run your Flask app on port 8080, you can set the PORT environment variable like this:
export PORT=8080
Conclusion
In this article, we've shown you how to change the default port that Flask uses to run your application. By following the steps outlined in this article, you can easily modify your Flask app to run on any port you like. With the use of environment variables, you can change the port number without having to modify your code, making it easier to run your Flask app on different ports on different machines.
here are some additional details that you may find helpful:
- Running Flask App on Different Ports
Flask runs on port 5000 by default. However, you can specify a different port by passing the port number as a parameter to the run()
method. For example:
if __name__ == '__main__':
app.run(port=8000)
This will run your Flask app on port 8000.
- Using Environment Variables to Set Port
Environment variables are a useful way to provide configuration information to your Flask app. You can use them to set the port number dynamically without hardcoding it in your code.
To use an environment variable to set the port number, use the os
module to retrieve the value of the environmental variable, as shown below:
import os
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(port=port)
In this example, we use the os.environ.get()
method to get the value of the "PORT" environment variable. If this variable is not set, it defaults to port 5000.
To set the environmental variable before running the Flask app, you can execute the following command in your terminal:
export PORT=8000
This will set the "PORT" environmental variable to 8000, and Flask will use this value to run the app.
- Using Multiple Ports in a Single Flask Instance
You may want to run multiple Flask instances on different ports in the same Flask app. This can be achieved using the flask.run_simple()
method.
For example, to run two instances of the Flask app, we can do:
from flask import Flask
app1 = Flask(__name__)
app2 = Flask(__name__)
@app1.route('/')
def index():
return "This is app1"
@app2.route('/')
def index():
return "This is app2"
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('localhost', 9000, app1)
run_simple('localhost', 9001, app2)
This will run two different instances of the Flask app; one on port 9000 and another on port 9001.
- Running Flask App on External Interface
By default, Flask runs on the "localhost" interface, which only allows access to your Flask app from your local machine. However, you can allow access to your Flask app from external interfaces by specifying the host parameter as "0.0.0.0".
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
This will allow access to your Flask app on port 8080 from any external interface.
Conclusion
Changing the port number of your Flask app is easy. You can do it by passing the port number as a parameter to the run()
method, or by using environmental variables to set it dynamically. Additionally, you can run multiple instances of your Flask app on different ports in the same Flask app. Finally, you can allow access to your Flask app from external interfaces by specifying the host parameter as "0.0.0.0".
Popular questions
-
How do I change the default port that Flask uses to run my app?
Answer: To change the default port that Flask uses to run your app, you can pass the 'port' parameter to therun()
method. For example:if __name__ == '__main__': app.run(port=8080)
will change the port to 8080. -
Can I use an environment variable to set the port number dynamically?
Answer: Yes, using an environment variable is a good way to set the port number dynamically. You can use theos
module to retrieve the value of the environment variable. For example:port = int(os.environ.get('PORT', 5000))
retrieves the value of PORT environment variable and defaults to 5000 if it's not set. -
Can I run multiple instances of my Flask app on different ports?
Answer: Yes, you can run multiple instances of your Flask app on different ports. You can achieve it using theflask.run_simple()
method and passing different instances of your Flask app to it. -
How can I run my Flask app on external interfaces?
Answer: Flask runs by default on the "localhost" interface, which only allows local access. To allow access to your Flask app from external interfaces, you should specify the host parameter as "0.0.0.0". For example:app.run(host='0.0.0.0', port=8080)
will allow access to your Flask app on port 8080 from any external interface. -
What is the default port number that Flask uses to run my app?
Answer: The default port number that Flask uses to run your app is 5000. If you don't specify a port number while running the Flask app, it will run on port 5000.
Tag
"Port-Modification"