close

[Solved] Python socket.error: [Errno 111] Connection refused

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python socket.error: [Errno 111] Connection refused in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python socket.error: [Errno 111] Connection refused Error Occurs?

Today I get the following error Python socket.error: [Errno 111] Connection refused in python.

How To Solve Python socket.error: [Errno 111] Connection refused Error ?

  1. How To Solve Python socket.error: [Errno 111] Connection refused Error ?

    To Solve Python socket.error: [Errno 111] Connection refused Error Well, the main reason would be there was no synchronisation whatsoever. Calling:

  2. Python socket.error: [Errno 111] Connection refused

    To Solve Python socket.error: [Errno 111] Connection refused Error Well, the main reason would be there was no synchronisation whatsoever. Calling:

Solution 1

The problem obviously was (as you figured it out) that port 36250 wasn’t open on the server side at the time you tried to connect (hence connection refused). I can see the server was supposed to open this socket after receiving SEND command on another connection, but it apparently was “not opening [it] up in sync with the client side”.

Well, the main reason would be there was no synchronisation whatsoever. Calling:

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

would just place the data into a OS buffer; close would probably flush the data and push into the network, but it would almost certainly return before the data would reach the server. Adding sleep after close might mitigate the problem, but this is not synchronisation.

The correct solution would be to make sure the server has opened the connection. This would require server sending you some message back (for example OK, or better PORT 36250 to indicate where to connect). This would make sure the server is already listening.

The other thing is you must check the return values of send to make sure how many bytes was taken from your buffer. Or use sendall.

(Sorry for disturbing with this late answer, but I found this to be a high traffic question and I really didn’t like the sleep idea in the comments section.)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read