Search code examples
flaskserverip

Flask server ip stuck to 127.0.0.1


Im currently trying to create a flask server with sqlite database. The issue is, I cannot make it run on a different ip than 127.0.0.1. I've tried 0.0.0.0, 192.168.1.222 and others

does anyone know why I cant change the port or ip address through code?

i tried changing the main:

if name == 'main': app.run(debug=True, host="192.168.1.222", port=5000)

but it doesnt care. With all that beeing said, if i run the same program from terminal using this: flask run -h 0.0.0.0 it runs with 2 ip addresses:

but not when I try change it from code


Solution

  • You should set the host to 0.0.0.0 change the default from 127.0.0.1 and open it up to non-local connections.

    It will run the application in all public address, including the 127.0.0.1 and your IP address of the machine on the network like you already said: 192.168.1.222

    From the flask docs:

    If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.

    If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

    $ flask run --host=0.0.0.0
    

    This tells your operating system to listen on all public IPs.

    Note that you may also need to make a firewall rule to allow external access to the port. If you are an ubuntu user:

    sudo ufw allow 5000/tcp