Skip to content
Home » socket.error: [Errno 111] Connection refused Python

socket.error: [Errno 111] Connection refused Python

To solve Python socket.error: [Errno 111] Connection refused error follow below methods.

ERROR LOG

File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

How to solve Python socket.error: [Errno 111] Connection refused ?

The issue was obvious (as you discovered) that port number was not open on the server side at the time you attempted to connect (hence connection refused). As far as I can tell, the server was intended to open this socket after receiving a SEND instruction on another connection, but it was “not opening [it] up in sync with the client side.”

The fundamental cause would be that there was no synchronisation at all. Calling:

cs.send("SEND " + FILE)
cs.close()

would simply store the data in an OS buffer; close would most likely flush the data and put it into the network, but it would very surely return before the data reached the server. Adding sleep after close may help to alleviate the issue, but this is not synchronisation.

The correct solution would be to ensure that the server has established the connection. This would necessitate the server sending you a response message (for example OK, or better PORT number to indicate where to connect). This ensures that the server is already listening.

Hope the above solution works.

Also read :

socket.error: [Errno 111] Connection refused Python
ImportError: No module named ‘ConfigParser’ Python
ImportError: No module named apt_pkg