Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: [Errno 9] Bad file descriptor in python 3

I'm a beginning/intermediate level programmer currently trying to write a simple web server in Python 3. However, whenever I run the module I get OSError: [Errno 9] Bad file descriptor. I've scoured the internet looking for answers, but I can't seem to figure this one out on my own. Here is the code and traceback:

#import socket module

from socket import *

serverSocket=socket(AF_INET,SOCK_STREAM)

#Prepare a server socket

serverSocket.bind(('IP address', 8000))
serverSocket.listen(1)

while True:
#Establish the connection

 print('Ready to serve...')

 (connectionSocket, addr) = serverSocket.accept()

 print ('connected from',addr)

 try:


      message=connectionSocket.recv(1024)
      filename=message.split()[1]
      print (filename)

      filename=message.split()[1]

      f=open(filename[1:])

      outputdata=f.read()

 #Send one HTTP header line into socket

      connectionSocket.send('HTTP/1.1 200 OK')

 #Send the content of the requested file to the client

      for i in range(0, len(outputdata)):
           connectionSocket.send(outputdata[i])
           connectionSocket.close()
 except IOError as err:
      print ('IO error')


           #Send response message for file not found

      connectionSocket.send(b'file not found')

                #Close client socket
      connectionSocket.close()
      serverSocket.close()

Traceback:

Traceback (most recent call last):
  File "/Users/BigRed/Desktop/SOS/webServer.py", line 17, in <module>
    (connectionSocket, addr) = serverSocket.accept()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line       184, in accept
fd, addr = self._accept()
OSError: [Errno 9] Bad file descriptor
like image 613
yosh315 Avatar asked Oct 27 '13 23:10

yosh315


People also ask

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 does Errno 9 mean?

connect:errno=9 - Indicates that the connection to the Proton server failed - errno could provide some additional clues about the reason.


1 Answers

When there is a OIError, you're calling serverSocket.close(). But when you re-enter in the while loop, you call serverSocket.accept() without call serverSocket=socket(AF_INET,SOCK_STREAM), and this fails, because you've called the close()

See this post

Hope help

PD: django developers don't use socket regularly. =)

like image 197
Leandro Avatar answered Sep 24 '22 15:09

Leandro