Can't connect to Flask web service, connection refused

PythonFlask

Python Problem Overview


I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:

> running on http://127.0.0.1:5000/

But when I enter this address on my laptop's in Chrome, I get

> ERR_CONNECTION_REFUSED

I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?

Python Solutions


Solution 1 - Python

Run your app like this:

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

It will make the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000

Solution 2 - Python

A reason could also be in firewall refusing incoming connections on port 5000. Try:

sudo ufw allow 5000

Solution 3 - Python

when you are running the server via flask run change it to flask run --host=0.0.0.0 to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000

Solution 4 - Python

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

if you run your app in this way then your server will be visible externally. Steps by Setp:

  1. Run your app by using the following command

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

  2. Go to the window cmd . Type ipconfig and get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X

  3. Go to the mobile browser and type the 192.168.X.X:5000

Solution 5 - Python

If you have debug = True inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the debug = True.

Solution 6 - Python

You will have to run the development server such that it listens to requests on all interfaces and not just the local one

Ask Flask to listen on 0.0.0.0:PORT_NUMBER

or any other port you may choose

Solution 7 - Python

  • Both devices must be connected to same network.
  • Use app.run(host='0.0.0.0',port=5000) and run with your own Ipv4 address like his http://[Your Ipv4 address]:5000
  • If you are connecting this with android app then don't forget to put INTERNET permission in manifest file.

Solution 8 - Python

Well i was also here to know answer but i guess i found out problem. The reason is that you may not activated or run flask that's why it is showing error. For that you have to start from terminal "flask run" and then surely your flask will run...

Solution 9 - Python

The issue may occur, if VPN is on, so try to switch it off.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYasharView Question on Stackoverflow
Solution 1 - PythonsalmanwahedView Answer on Stackoverflow
Solution 2 - PythonArtem ZaytsevView Answer on Stackoverflow
Solution 3 - PythonSam BernsteinView Answer on Stackoverflow
Solution 4 - PythonMuhammad UsmanView Answer on Stackoverflow
Solution 5 - PythonmouseratView Answer on Stackoverflow
Solution 6 - PythonHitesh DharamdasaniView Answer on Stackoverflow
Solution 7 - PythonEhtasham AliView Answer on Stackoverflow
Solution 8 - PythonSwapnil WatkarView Answer on Stackoverflow
Solution 9 - PythonTony YusupovView Answer on Stackoverflow