Changing Host IP Address in Flask
In this article, we will cover how we can change the host IP address in Flask using Python. The host IP address is the network address that identifies a device on a network. In the context of a Flask application, the host IP address is the address that the application listens to for incoming requests. By default, Flask applications listen on the localhost address 127.0.0.1:5000, which means they can only be accessed from the same machine that the application is running on.
However, it is often useful to be able to access the application from other devices on the same network, or even from the internet. In these cases, the host IP address can be changed to allow the application to be accessed from other devices. This is done by specifying the host IP address in the app.run() function of the Flask application.
Changing the IP address in a Flask application using the “host” parameter
Here are the steps for changing the host IP address in a Flask application using the “host” parameter in the app.run() function. Open the Flask application in a text editor. Locate app.run() function in the main script file. Add the “host” parameter to the app.run() function, followed by the desired IP address, such as: app.run(host=’192.168.0.105′) this is the local address of your system. This will only allow the application to be accessed from the specified IP address.
Changing IP from the command line while deploying the Flask app
Here, the app.run() function does not specify an IP address or a port, so it will use the defaults of localhost (127.0.0.1) and port 5000. You can run this application by setting the FLASK_APP environment variable to the name of your application file and then using the flask run command and then you can change the IP address and port number while running the command
set FLASK_APP=app.py flask run
flask run --host=192.168.0.105 --port=5000
This will run the server on IP address 192.168.0.105 and port 5000.
Please Login to comment...