Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 257
Alex Avatar asked Jun 20 '13 06:06

Alex


People also ask

What does [errno 9] bad file descriptor mean?

OSError: [Errno 9] Bad file descriptor It means that the file defined in the program is already closed automatically while running the code. There lies no use in defining a separate method to perform the same task again.

What is bad file descriptor in Python socket?

[Errno 9] Bad File Descriptor in Python Socket Module Another main area in which this error is seen is in the Python socket – Socket error Bad file descriptor. When dealing with this kind of program, you can notice that you will find a Bad file descriptor error message is seen along with some issues in opening/closing or accessing the socket.

Why is my file descriptor not working in Unix?

Make sure you are using a valid file descriptor number. You will get a UNIX or Python Shell- Bad file descriptor error message when you fail using the right file descriptor number. This can cause issues when you open, close or use a file. Use the right modes while handling file descriptors.

What are errors in Python programming?

In Python, errors generally occur when a particular code segment is not in compliance with the advised usage. Various errors are commonly faced by programmers, which include indentation, syntax, etc. Rectifying these errors is no big deal when you review your code thoroughly.


1 Answers

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

like image 170
Atmaram Shetye Avatar answered Oct 21 '22 11:10

Atmaram Shetye