title: Python TCP Server
body: <p>I wrote a simple Python 3 TCP multithreaded server and telnet to it. All seems to work until I go to close the telnet connection. When I close the telnet connection ^] and then control+d to exit telnet the server repeatedly prints ''.</p>
  <p>Is there something trivial I am missing here to prevent this behavior?</p>
  <pre><code>import socket 
  from threading import Thread 
  from socketserver import ThreadingMixIn 
   
  # Multithreaded Python server : TCP Server Socket Thread Pool
  class ClientThread(Thread): 
   
      def __init__(self,ip,port): 
          Thread.__init__(self) 
          self.ip = ip 
          self.port = port 
          print (&quot;[+] New server socket thread started for &quot; + ip + &quot;:&quot; + str(port)) 
   
      def run(self): 
          while True : 
              data = conn.recv(2048) 
              #data = &quot;Echo data: &quot; + data.decode()
              print (&quot;Server received data: &quot;, data)
              conn.send(data)  # echo 
   
  # Multithreaded Python server : TCP Server Socket Program Stub
  TCP_IP = '0.0.0.0' 
  TCP_PORT = 2004 
  BUFFER_SIZE = 1024  # Usually 1024, but we need quick response 
   
  tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
  tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
  tcpServer.bind((TCP_IP, TCP_PORT)) 
  threads = [] 
   
  while True: 
      tcpServer.listen(4) 
      print (&quot;Multithreaded Python server : Waiting for connections from TCP clients...&quot;) 
      (conn, (ip,port)) = tcpServer.accept() 
      newthread = ClientThread(ip,port) 
      newthread.start() 
      threads.append(newthread) 
   
  for t in threads: 
      t.join() 
  
  
  
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received data:  b''
  Server received da
  </code></pre>