errno 9 bad file descriptor, basic python server socket
Question: I'm having difficulty with getting data from a server in python. I'm getting
Errno 9 bad file descriptor and a 'connection was reset' message on my browser
whenever I try to access my test html file. The code is as follows:

    
    
    #import socket module
    from socket import *
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverPort = 12000
    #Prepare a sever socket
    serverSocket.bind(("", serverPort))
    serverSocket.listen(1)
    
    
    while True:
        #Establish the connection
        print 'Ready to serve...'
        connectionSocket, addr = serverSocket.accept()#Accepts a TCP client    connection, waiting until connection arrives
        print 'Required connection', addr
        try:
    
            message = connectionSocket.recv(64)
            filename = message.split()[1]
            f = open(filename[1:])
            outputdata = f.read()
    
            #Send one HTTP header line into socket
            connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n')
    
    
            #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:
            #Send response message for file not found
            connectionSocket.send('404 Not Found!')
            #Close client socket
            connectionSocket.close()
            serverSocket.close()
    

I can't tell why i am getting this error. I have tried removing the close from
right outside the outputdata for loop, which didn't work either. I tried
changing the server port, and closing the socket and server in different
orders.

the full trackback is:

    
    
    Traceback (most recent call last):
      File "UDPServer.py", line 13, in <module>
        connectionSocket, addr = serverSocket.accept()#Accepts a TCP client   connection, waiting until connection arrives
      File "C:\Anaconda\lib\socket.py", line 202, in accept
        sock, addr = self._sock.accept()
      File "C:\Anaconda\lib\socket.py", line 170, in _dummy
        raise error(EBADF, 'Bad file descriptor')
    socket.error: [Errno 9] Bad file descriptor
    


Answer: You can't use the socket once it's closed. [The
docs](http://docs.python.org/2/library/socket.html#socket.socket.close) for
`socket.close()` say:

> All future operations on the socket object will fail.

You could create a new socket in the loop.

