How to Run Host 0.0.0.0 in Python Flask

Haider Ali Feb 02, 2024
  1. the app.run() Function
  2. Use debug Argument
  3. Use port Argument
  4. Use host Argument
  5. Use All Arguments in app.run()
How to Run Host 0.0.0.0 in Python Flask

Here, in this guide, we will examine the question: What does app.run(host 0.0.0.0) mean?

What are its conditions to run and understand them? So let us not wait any further and start with this guide.

the app.run() Function

Let us look into this with some simple explanation so we can further understand the working of the app.run(host=0.0.0.0). The app.run() function allows one to run an application.

It can run with or without any arguments. It will consider the arguments as its default state if it contains no arguments.

Some of the arguments we will discuss in this guide, and use in this function are host, port, and debug.

Use debug Argument

This argument is used if there is a need for any changes that need to be made in our code.

By default, the argument debug is already set to false(off), so any changes made will not be saved; hence, the user would have to restart the server.

Note that keeping the debug argument to True(on) should only be done while working with local machines.

If debug is turned to True while deploying the server, there is a high chance that your server will be hacked as the code is visible to anyone who might make any changes or if there is an error.

Use app.run()

This is an example code that will be used throughout this guide. Here we use the above function, app.run().

As you can see, we used debug as the argument and set it to True. This will now enable us to make our desired changes.

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    return "<p>Hello!!!</p>"


if __name__ == "__main__":
    app.run(debug=True)

Use CLI

The other method to use debug is by CLI(Command Line Interface). We can switch debug to True by simply typing this command.

We can use any code and make these changes while executing.

flask --app <app_name> --debug run

For example, the above code is named f.py. Then this command will be used as:

flask --app f --debug run

This output is for both of the above methods.

debug argument

Use port Argument

To change the port number of our application, we use this argument. But by changing the port, we would have to restart the server, or else it will keep working on the previous port.

We use this port when there is a need to run multiple apps on the machine.

In this case, there is a chance that the port that we are working on might be busy. So to overcome this problem we use another port.

Use app.run()

Using the above code, we will make some changes to it to use the port argument as desired.

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    return "<p>Hello!!!</p>"


if __name__ == "__main__":
    app.run(port=5002)

Here, the port number is changed to 5002.

Use CLI

By using CLI commands, we can type this command to change the port number to 5002.

flask --app <app_name> run --port=5002

For example, the above code is named f.py. Note that, as mentioned above, we can use any code and these tags while executing.

flask --app f run --port=5002

This is the output for both of the methods mentioned above.

port agument

As you can see, the port number is now changed to 5002.

Use host Argument

Suppose we are in a team of developers of about 5-6 members. While working in teams, the members have to communicate with each other by displaying their progress.

Now imagine that you are in your office working when you have to display your progress with your team but cannot since one of your team members is not present.

This is where Flask’s feature, host="0.0.0.0" come in. Through this feature, a network is created as a virtual server where the member of your team can access the application through an IP address.

That IP address can work on any device; hence, it is portable and time-saving.

Use app.run()

Here is an example that allows others to view the same page on various devices using host='0.0.0.0'.

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    return "<p>Hello!!!</p>"


if __name__ == "__main__":
    app.run(host="0.0.0.0")

Use CLI

By simply typing this command, we can get the IP address to share with others. The f is a random file name, f.py that we chose for this example.

flask --app <app_name> run --host=0.0.0.0

flask --app f run --host=0.0.0.0

Output:

host argument

This is the output shown when we use either of these methods. As you can see above, there is an additional IP address provided.

The second IP address is the one we can share with other devices; it can be a PC, laptop, or even mobile phone.

Now let us try using all these arguments together.

Use All Arguments in app.run()

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    return "<p>Hello!!!</p>"


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5002, debug=True)

Use CLI

flask --app f --debug run --host=0.0.0.0 --port=5002

Keep in mind, always use the command in this order, or else the command will not execute properly, or it might not even execute.

all arguments

As you can see in the above output, debug is switched to true, the port number has changed to 5002, and we have been provided another IP address to view the page on separate devices.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn