title: Python Socket Programming - Bad file descriptor error
body: <p>I'm trying to create a simple server, and keep getting the following error in IDLE:</p>
  
  <pre><code>  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')
  error: [Errno 9] Bad file descriptor
  </code></pre>
  
  <p>This is my code. I've tried understanding why, and it has something to do with closing one of the sockets and then trying to use it again but I don't really get how I'm supposed to fix it. Any and all help is appreciated. :)</p>
  
  <pre><code>from socket import *
  serverSocket = socket(AF_INET, SOCK_STREAM)
  port=10101
  serverSocket.bind(('',port))
  serverSocket.listen(5)
  while True:
          print 'Ready to serve...'
          connectionSocket, addr = serverSocket.accept()
          try:
                  message = serverSocket.recv(1024)
                  filename = message.split()[1]
                  f = open(filename[1:])
                  outputdata = f.read()
                  connectionSocket.send("HTTP/1.1 200 OK\r\n\n")
                  for i in range(0, len(outputdata)):
                          connectionSocket.send(outputdata[i])
                  connectionSocket.close()
          except IOError:
                  connectionSocket.send('HTTP/1.1 404 File not found\r\n\n')
                  connectionSocket.close()
                  serverSocket.close()
  serverSocket.close()
  </code></pre>