Saturday, November 16, 2024
Google search engine
HomeLanguagesChanging Host IP Address in Flask

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.

Python3




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

Changing Host IP Address in Flask

 

Changing Host IP Address in Flask

 

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

Python3




from flask import Flask
  
app = Flask(__name__)
  
@app.route('/')
def hello():
    return 'Hello, World!'
  
if __name__ == '__main__':
    app.run()


Output:

This will run the server on IP address 192.168.0.105 and port 5000.

Changing Host IP Address in Flask

 

Changing Host IP Address in Flask

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments