Connect to Flask Server from other devices on same network

Question:

Dear smart people of stackoverflow,

I know this question has been asked a lot here but none of the posted solutions have worked for me as of yet. Any help here would be much appreciated:

The Problem: Cannot connect to flask app server from other devices (PCs, mobiles) on the same network. (in other words: localhost works perfectly but I cannot connect from external device)

What I’ve Tried:

1) Setting app.run(host='0.0.0.0', port=5000, debug=True, threaded=True) in the app.py so that the server will listen on all available network interfaces.

2) Enabling TCP traffic for port 5000 in local network in Windows Defender Firewall (with inbound and outbound rules added)

3) Using my host PC’s IPv4 address in the URL bar of my external device’s browser in the following format: http://<host_ipaddress>:<port>/

4) Using my host PC’s hostname in the URL bar of my external device’s browser in the following format: http://<host_name>:<port>/

5) Running the app.py file from Windows Powershell and Python (.py) Executor

None of these solutions has worked so far, even after attempting to connect from a few different external devices. Thanks in advance for your help!

Asked By: rmd_po

||

Answers:

I did a similar setup with my django project. Your PC network settings are probably all good, but your router ( at least for wlan) is blocking the traffic. I solved this by tampering with needed settings in router manager api, which can be found from 192.168.1.1 in your local network. You can check your device ip addresses etc.

Answered By: potato_cannon

Much depends on how you’re running your app. From what you’ve written, I’m guessing that you have

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)

If that’s not working, changes are good that you’re using flask run to start things up. The problem here is that flask will import your application before looking for app in your application’s namespace. The problem is that __name__ will then reflect the name of the base file, and not __main__, so app.run() never gets run.

If that’s the case, try passing --host=0.0.0.0 as an argument.

Answered By: Dave W. Smith

I solved the issue by changing my home network profile to private instead of public, which allows my PC to be discoverable. Completely overlooked that!

Hope this helps someone!

Answered By: rmd_po

Here is the method which worked for me.

  1. Find the ip address of system using ifconfig command
  2. Replace the host ip in code with your ip

server.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def about():
    return 'It worked!'

if __name__ == '__main__':
    app.run(host='192.168.43.81', port=5000, debug=True, threaded=False)

To run the program:
python3 server.py

I know it is late, but I believe it can help others.

Answered By: Rajendra Prajapat

If you are on windows go to your internet settings and change network profile from Public to private
enter image description here

Answered By: Epsi95

Go to your wifi settings and change network profile from public to private.

If its already private click on configure firewall and security settings shown below which opens windows security

There disable the microsoft defender firewall for private connections.enter image description here

This worked for me

Answered By: rocker arpan

I know this is years late , but for anyone else who might have trouble…,

You can write this code

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

and run the app using python and not flask run. If you run the app with flask run the name of app will never be main so the app will never run on host 0.0.0.0.

Also change your WiFi from public to private.

Answered By: Davis Omogi