The following receiveFile()
function reads a filename and file data from the socket and splits it using the delimiter $
.
But I am unable to close the socket and a Bad file descriptor
error is raised. If I comment out the self.server_socket.close()
statement then there is no error but the socket is listening forever.
Code:-
def listen(self):
self.server_socket.listen(10)
while True:
client_socket, address = self.server_socket.accept()
print 'connected to', address
self.receiveFile(client_socket)
def receiveFile(self,sock):
data = sock.recv(1024)
data = data.split("$");
print 'filename', data[0]
f = open(data[0], "wb")
#data = sock.recv(1024)
print 'the data is', data[1]
f.write(data[1])
data = sock.recv(1024)
while (data):
f.write(data)
data=sock.recv(1024)
f.close()
self.server_socket.close()
print 'the data is', data
print "File Downloaded"
Traceback:-
Traceback (most recent call last):
File "server.py", line 45, in <module>
a = Server(1111)
File "server.py", line 15, in __init__
self.listen()
File "server.py", line 20, in listen
client_socket, address = self.server_socket.accept()
File "c:\Python27\lib\socket.py", line 202, in accept
sock, addr = self._sock.accept()
File "c:\Python27\lib\socket.py", line 170, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa). There are no child processes. This error happens on operations that are supposed to manipulate child processes, when there aren't any processes to manipulate.
Reason for getting error “oserror: [errno 9] bad file descriptor” : The internal state of the file object indicates the file is still open since f. close() was never called, so the destructor tries to close the file. The OS subsequently throws an error because of the attempt to close a file that's not open.
When you don't allow the code to perform the functions related to the file descriptors and the methods used, a Bad File Descriptor Error arises in Python, indicating the wrong way of implementing the code.
[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.
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.
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, file descriptors are integers (positive) that identify the kernel’s open files kept in a table of files. They are generally non-negative values. If found to be negative, that indicates error or a “no value” condition.
You are closing the server's listening socket, and after that calling again accept() on it. To finish receiving one file you should close client connection's socket (sock in function receiveFile).
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