-3

The Flask server is up and running on my laptop (Ubuntu), with debugger ON:

(venv) deeman@carbon:~/flask_dir/venv/dox$ flask --app hello_w run 
 * Serving Flask app 'hello_w'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 130-661-482

Bellow is the basic server page in python

app = Flask(__name__)

@app.route("/")
@app.route("/hello")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route("/test")
def hello_world2():
    return "<p>Test works!</p>"
    
if __name__ == "__main__":
    app.debug = True
    app.run(host="0.0.0.0", port = 5000)

On any other devices connected to the same Wifi network I get Can't connect to Server or Site can't be reached

What am I missing? Can someone help me understand issue? Thanks

davidism
  • 121,510
  • 29
  • 395
  • 339
deeman
  • 1
  • 2
  • What IP are you trying to connect to? A classic mistake is trying to connect via 127.0.0.1 through a remote machine. If that is the case, read here: https://linuxhint.com/meaning-of-127-0-0-1/ – Nadav Barghil Nov 13 '22 at 19:18
  • I tried them all, tbh. @NadavBarghil (see the updates above with all ```ifconfig```): 192.168.0.196:5000/ 172.16.32.1:5000/ 172.16.110.1:5000/ 127.0.0.1:5000/ Nothing works from any device other than my laptop/server – deeman Nov 13 '22 at 19:45
  • Try adding `--host=0.0.0.0` as mentioned in https://flask.palletsprojects.com/en/1.1.x/quickstart/ – Dave W. Smith Nov 13 '22 at 19:50
  • @DaveW.Smith worked!! But I don't understand why. I already mentioned ```app.run(host="0.0.0.0", port = 5000``` in the server.py code. Any idea why I have to specify host=0.0.0.0 again? in this way:```$ flask --app hello_w run --host=0.0.0.0``` – deeman Nov 13 '22 at 19:56

1 Answers1

-1

Thank you all. @DaveW.Smith provide the solution in my case, which was: to simply pass --host=0.0.0.0 as an argument, as bellow: $ flask --app hello_w run --host=0.0.0.0

deeman
  • 1
  • 2
  • It's a Python thing. The `if __name__ == '__main__':` block only executes if you invoked the file via `python` (or `python3`). If you're using the `flask` command, the file gets imported, and `__name__` isn't `'__main__'`. – Dave W. Smith Nov 14 '22 at 05:19