Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket (Socket Error Bad File Descriptor)

Tags:

python

sockets

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
like image 796
Ali Ahmad Avatar asked May 05 '13 08:05

Ali Ahmad


People also ask

What causes 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.

What is Errno 9 Bad file Descriptor?

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.

What is bad file descriptor in Python?

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.

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.

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.

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 is a negative file descriptor in Python?

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.


1 Answers

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).

like image 94
Nickolay Olshevsky Avatar answered Sep 17 '22 11:09

Nickolay Olshevsky