title: How to write a python echo server that doesn't disconnect after first echo?
body: <p>I want to set up a simple echo server that just echoes back whatever the client sends to it. However, currently the server disconnects (the server socket closes) after it echoes back the first client message. I want to be able to "chat" continuously with the server, where the server just echoes back several consecutive messages I send without disconnecting; e.g.:</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>"Hi there!"</p>
        
        <p>"Echoing: Hi there!"</p>
        
        <p>"How are you?"</p>
        
        <p>"Echoing: How are you?"</p>
        
        <p>"Cheers!"</p>
        
        <p>"Echoing: Cheers!"</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>etc.</p>
  
  <p>Currently I have the following code:</p>
  
  <p>server.py:</p>
  
  <pre><code>import socket
  
  HOST = '127.0.0.1' 
  PORT = 5000
  
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.bind((HOST, PORT))
      s.listen()
      conn, addr = s.accept()
      with conn:
          while True:
              data = conn.recv(1024)
              if not data:
                  break
              conn.sendall(data)
  </code></pre>
  
  <p>client.py:</p>
  
  <pre><code>import socket
  
  HOST = '127.0.0.1'
  PORT = 5000
  
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((HOST, PORT))
      s.sendall(b'Hello, world')
      data = s.recv(1024)
  
  print('Echoing: ', repr(data))
  </code></pre>
  
  <p>The server, however, disconnects after it echoes back the first client message (probably because of the <code>if not data: break</code> statement).</p>
  
  <p>P.S. I'd appreciate any additional explanations which might be necessary - this example has educational purposes, so I'm not (only) after getting the code running.</p>
  
  <p>Thanks!</p>