How to Solve ConnectionRefusedError: [Errno 111] Connection Refused in Python

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

As a versatile programming language, Python can also be used to create a networked application. Game servers, web servers, microservices, and instant messaging are some of the possible use cases for Python.

With Python, you can build a client app that connects to a certain server to do a certain thing (such as a third-party music streaming client) or a server that accepts connections from clients (such as a web server for dynamic health data processing).

Or, even better, to avoid having to use multiple tech stacks, why not create servers and clients entirely using Python? It’s certainly doable and even practical in some cases.

Python handles network connections gracefully, and if you develop with Python, you can specify the type of communication you want in your application. Uploading and downloading files is also a breeze with Python apps.

That being said, there are a few cases where developers/users can’t get into their web/services due to the error “ConnectionRefusedError: [Errno 111] Connection refused”. It can lead users that there is something wrong with the apps. We’re going to show you how to solve this issue.

Table of Contents

What Caused the Error?

This error is caused by the client’s inability to access the specified port in the computer or network. Sometimes, ports are blocked in school or library networks to avoid illicit communication. This is especially true if your port is far from the commonly-used ports.

How to Fix It?

If you know the port number of your Python application, you can try whitelisting the port in your own network or asking the school/library technician to allow your specific port.

This error can also happen if the server is offline or not accepting a connection. In this case, you will need to start the server manually in the meantime.

Services such as Pingdom make it easy to monitor server availability, or you can explore things such as Cron jobs to monitor your service. However, if possible, reduce things that run in your service to maintain good security posture and application performance.

In short, when you’re facing the error “ConnectionRefusedError: [Errno 111] Connection refused”, it is a normal thing. A reboot of your computer might solve the problem.

If it is not fixed, you might need to whitelist the used port in the firewall. Or, if the problem persists, it is time to take a look at your server(s).

Are they overwhelmed? Do you need new equipment? It is a question only you, as a developer, could answer.

Leave a Comment