1

This is my app.py file:

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"

app.run(host = '192.168.2.18', debug=False)

I've used the netstat command in Windows and that's how I know to use '192.168.2.18' When I run the file, it appears to run on port 5000

Please see pic below:

App runs!

However, when I try to access this url from another device over the network, I don't see it. I only see it on my machine.

I've tried various solutions most notably from this popular thread:

Configure Flask dev server to be visible across the network

3 Answers3

0

If you update the host ip in your code to 0.0.0.0, then your app will start listening all the public IPs.

app.run(host='0.0.0.0')

You can read here in the flask docs.

Now for someone to access your page, they have to navigate to this url -

http://your.machine.ip:5000/

You can also give any specific port you want your application to run at, lets say 4000 (default port is 5000)

app.run(host='0.0.0.0', port=4000)

You can even give your machine name in place of your machine ip in the url, just incase your ip is not static.

Ejaz
  • 1,504
  • 3
  • 25
  • 51
0

I tell you a secret, run:

python3 -m http.server

and the directory in which you are will be available through all the network. In this example I'm visiting my Pictures folder and as you can see is available at http://0.0.0.0:8000/

enter image description here

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
0

Since you have mentioned, you already tried the solutions from this post, here's something I think you can try because I was facing this same problem once and this is what I was missing, and it's overlooked a lot of times. If you are a Windows user, check your Network and sharing settings, and turn it ON for sharing on a private network. Something like this:

enter image description here

Also, make sure your IP address in the code below is correct (because IP address changes all the time when you connect your laptop/computer over Wi-Fi), so check the current IP by running ipconfig in cmd right before running your code, not the one you wrote a week ago.

Few changes in the code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello World!"

if __name__ == "__main__":
    #app.run(host='0.0.0.0',port=5000)   //doesn't work for me
    app.run(host='192.168.2.18', port=5000)

And finally, run your code with flask run -h 192.168.X.X, in the screenshot you shared, you entered flask run --host 192.168.X.X, I m not sure why, but --h doesn't work for me. And, I hope you also check you are entering the address properly, like http://192.168.2.18:5000, not https. Sometimes silly stuff can get in the way too :|

Singh
  • 504
  • 4
  • 15