pythonsocketspython-2.7

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.


Solution

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