By default, Flask runs on 127.0.0.1:5000, which means it can only be accessed from the same machine. However, we may want to access our Flask app from other devices on the same network or even from the internet. To do this, we need to change the host IP address.
Changing the IP address in a Flask application using the "host" parameter
To make the Flask app accessible on a different IP, update the app.run() function with the host parameter.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World! this application runing on 192.168.0.105'
if __name__ == '__main__':
app.run(host='192.168.0.105')
Output:


Now, the app will be accessible from other devices on the same network using 192.168.0.105:5000.
Changing the IP Address via Command Line
Instead of modifying the script, we can set the host and port directly when running the app from the terminal. Here are the steps:
1. Set the Flask app environment variable:
set FLASK_APP=app.py
2. Run the Flask app with a custom IP and port:
flask run --host=192.168.0.105 --port=5000
Example: Default Flask App
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
This will run the server on IP address 192.168.0.105 and port 5000. Below are the snippets:


We can make it accessible on a different IP by using the command "flask run --host=192.168.0.105 --port=5000".