title: How to keep a socket open until client closes it?
body: <p>I have simple python server and client.</p>
  
  <p>Server:</p>
  
  <pre><code>import SocketServer
  import threading
  
  
  class MyTCPHandler(SocketServer.BaseRequestHandler):
      def handle(self):
          self.data = self.request.recv(1024).strip()
          print str(self.client_address[0]) + " wrote: "
          print self.data
          self.request.send(self.data.upper())
  
  
  if __name__ == "__main__":
      HOST, PORT = "localhost", 3288
      server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
      server.serve_forever()
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>import socket
  import sys
  from time import sleep
  
  HOST, PORT = "localhost", 3288
  data = "hello"
  
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  try:
      sock.connect((HOST, PORT))
      sock.send(data + "\n")
      received = sock.recv(1024)
  
      sleep(10)
  
      sock.send(data + "\n")
      received = sock.recv(1024)
  
      sleep(10)
  
      sock.send(data + "\n")
      received = sock.recv(1024)
  
  finally:
      sock.close()
  </code></pre>
  
  <p>Here is the output I get:</p>
  
  <p>Server:</p>
  
  <pre><code>&gt;python server.py
  127.0.0.1 wrote:
  hello
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>&gt;python client.py
  Traceback (most recent call last):
    File "client.py", line 18, in &lt;module&gt;
      received = sock.recv(1024)
  socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
  </code></pre>
  
  <p>I tried it on a linux machine as well. The server only receives one message and then I get an error on the recv statement of second message. I have just started learning networking on python but I think the server is closing the socket for some reason. How do I correct this?</p>