Alex
Alex

Reputation: 44325

What could be the reason for a socket error "[Errno 9] Bad file descriptor"

I have a complex python (2.7.3) script which is trying to open a socket connection via

self.socket.close()
# doing something else
self.socket.connect((host, port))

but all I get is the following socket error:

error: [Errno 9] Bad file descriptor

The host:port accepts connections as I have verified this with nc host port manually. So what could be the possible reasons I get this error for opening a connection to the given port, which actually works?

I cannot and will not post the full script as it is too complex and irrelavent for this question. I just would like to know all possible reasons for this error, and how to check them and fix them.

Upvotes: 11

Views: 52929

Answers (2)

user28994890
user28994890

Reputation: 1

Windows 10. When using idle, I got this error message. Turns out idle does not have write permission in the folder. Solution: Run idle as administrator or give idle write permission. In windows security, go to Virus & threat protection -> manage Ransomware protection -> Allow an app through Controlled folder access + Add an allowed app

Upvotes: 0

Atmaram Shetye
Atmaram Shetye

Reputation: 1013

You will need to create a new socket object. Maybe self.socket = socket.socket() after closing the previous socket and before connecting.

Upvotes: 27

Related Questions