Fixing Flask issue – Can’t connect to Flask web service, connection refused ( ERR_CONNECTION_REFUSED )

Flask issue – Can’t connect to Flask web service, connection refused ( ERR_CONNECTION_REFUSED ) is an error which occurs when a connection is refused while using flask to run a server.

In today’s blog post I am going to present an annoying and confusing python error and explain why this error is taking place and how to fix it, with a set of possible fixes.

Exploring the Flask issue – Can’t connect to Flask web service, connection refused ( ERR_CONNECTION_REFUSED )

This is an error which occurs when a connection is refused while using flask to run a server.

Beware of mixing between errors. Double check if the error message looks like the error message bellow, then continue.

                                                                       #
ERR_CONNECTION_REFUSED
                                                                       #

My Step by Step Method : Try using flask run and/or change your firewall permissions

The first solution is to use flask run, if you you use flask run to run your server, you should try running your server using 

                                                                       #
flask run --host=0.0.0.0
                                                                       #

This is going to make your server visible externally, now your server listens to requests on all interfaces and does not only listen to your local network.

If that does not work try

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

to establish the connection you should be on the same network and go to

                                                                       #
http://network IPV4:5000
# for example http://192.168.X.X:5000
                                                                       #

If your firewall blocks incoming connections on the port, you should allow the connection using the command bellow, while 5000 is the port.

                                                                       #
sudo ufw allow 5000
                                                                       #

This is only for android users :

If you are connecting this with an android app Add the INTERNET permission to your manifest file.

Find AndroidManifest.xml and outside the application tag, add this line

                                                                       #
<uses-permission android:name="android.permission.INTERNET" /> 
                                                                       #

I hope this guide solved your problem, thank you for reading. Thank you for reaching the end of this blog post.

Summing-up : 

This is the end of our post, I hope this has been helpful and helped you solve the error or at least pointed you in the right direction.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/