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.
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.
[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.
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.
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.
You will need to create a new socket object. Maybe self.socket = socket.socket()
after closing the previous socket and before connecting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With