How to Fix ConnectionRefusedError: [Errno 111] Connection Refused

Zeeshan Afridi Feb 02, 2024
  1. Why the ConnectionRefusedError: [Errno 111] Connection refused Occurs in Python
  2. How to Solve the ConnectionRefusedError: [Errno 111] Connection refused in Python
  3. Conclusion
How to Fix ConnectionRefusedError: [Errno 111] Connection Refused

This error indicates that the client cannot connect to the port on the server script’s system. Since you can ping the server, it should not be the case.

This might be caused by many reasons, such as improper routing to the destination. The second possibility is that you have a firewall between your client and server, which may be either on the server or the client.

There shouldn’t be any routers or firewalls that may stop the communication since, based on your network addresses, both the server and the client should be on the same Local Area Network.

Why the ConnectionRefusedError: [Errno 111] Connection refused Occurs in Python

This error arises when the client cannot access the server because of an invalid IP or port or if the address is not unique and used by another server.

The connection refused error also arises when the server is not running, so the client cannot access the server as the server should accept the connection first.

Code example:

# server code
import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind((host, port))

s.listen(5)
while True:
    c, addr = s.accept()
    print("Got connection ", addr)
    c.send("Meeting is at 10am")
    c.close()
# client code
import socket

s = socket.socket()
host = "192.168.1.2"
port = 1717

s.connect((host, port))
print(s.recv(1024))
s.close

Output:

socket.error: [Errno 111] Connection refused

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

Try to keep the receiving socket as accessible as possible. Perhaps accessibility would only take place on one interface, which would not impact the Local Area Network.

On the other hand, one case can be that it exclusively listens to the address 127.0.0.1, making connections from other hosts impossible.

Code example:

import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind(("", port))

s.listen(5)
while True:
    c, addr = s.accept()
    print("Got connection ", addr)
    c.send("The Meeting is at 10 am")
    c.close()
import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind(("", port))

s.connect((host, port))
print(s.recv(1024))
s.close()

Output:

Got connection('192.168.1.2')
The meeting is at 10 am

When you run the command python server.py, you will receive the message Got connection. At the same time when you run the command python client.py, you will receive a message from the server.

The DNS resolution can be the other reason behind this problem. Since the socket.gethostname() returns the hostname, an error will be returned if the operating system cannot translate it to a local address.

The Linux operating system can edit the host file by adding one line.

host = socket.gethostname()
port = 1717
s.bind((host, port))

Use gethostbyname

host = socket.gethostbyname("192.168.1.2")
s.bind((host, port))

Thus, you must use the identical technique on the client and server sides to access the host. For instance, you would apply the procedure described above in a client’s case.

You can also access through local hostname hostnamehost = socket.gethostname() or specific name for local host host = socket.gethostbyname("localhost").

host = socket.gethostname()
s.connect((host, port))
host = socket.gethostbyname("localhost")
s.connect((host, port))

Conclusion

ConnectionRefusedError in Python arises when the client cannot connect to the server. Several reasons include the client not knowing the IP or port address and the server not running when the client wants to connect.

There are several methods mentioned above to resolve this connection issue.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error