Bad file descriptor socket and threading
<p>I try to create a simple client and a server that work with threading. But sometimes my server prints an error : Bad file descriptor. I see on the web the same problem and the solution to resolve the problem. But finally, I don't success to resolve my problem. So if you have any idea how to solve the problem it will be graceful.
Thanks !</p>

<p>My code:</p>

<pre><code>import socket               # Import socket module
from threading import Thread

# Global Variable


class ThreadServer:
    def __init__(self, host, port):
        try:
            self.host = host
            self.port = port
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.bind((self.host, self.port))
            print "Server Start successfully !"
        except Exception as e:
            print "Something wrong %s" % (e)

    def listen(self):
        self.sock.listen(3)  # Waiting for n client
        while True:
            client, address = self.sock.accept()  # Establish connection with clients
            client.settimeout(60)
            Thread(target=self.listen_for_client, args=(client, address)).start()

    def listen_for_client(self, client, address):
        print "Got connection from",  address
        while True:
            try:
                msg = client.recv(1024)
                if msg[0:4] == "USER":
                    user = msg[4:]
                    self.write_new_clients(address, user)
                    msg = 0
                if msg:
                    print msg
                    client.send("Server : " + msg)
            except Exception as e:
                print "Something wrong %s" % (e)
                client.close()

    def write_new_clients(self, address, user):
        my_file = open("Clients_list.txt", "a+")
        line = my_file.readline()
        for i in line:
            if str(address) in line:
                print "Already Exist !"
            else:
                my_file.write(str(user) +"::" + str(address)  + "\n")


def main():
    ThreadServer("0.0.0.0", 8350).listen()


if __name__ == "__main__":
    main()
</code></pre>