title: Python Non-blocking peer to peer chat socket.error: [Errno 32] Broken pipe
body: <p>Hi i write simple chat program for peer to peer chat between server and client.
  This code is working for Client side and client can send message and server recives that messages. but for server side when i want to send a message i have error in line 40 </p>
  
  <pre><code>  File "server.py", line 40, in &lt;module&gt;
      newSocket.send('\r&lt;Server&gt;: ' + msg)
      socket.error: [Errno 32] Broken pipe
  </code></pre>
  
  <p>and server crashes.</p>
  
  <p>Server :</p>
  
  <pre><code>import socket
  import os
  import select
  import sys
  
  def prompt():
      sys.stdout.write('Server :  ')
      sys.stdout.flush()
  
  try:
      newSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      newSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  except:
      print 'socket Error'
      sys.exit(1)
  
  newSocket.bind(('127.0.0.1', 8000))
  newSocket.listen(5)
  
  input_list = [newSocket, sys.stdin]
  
  print 'Chat Program'
  prompt()
  
  while True:
  
      inputready, outputready, exceptready = select.select(input_list,[],[])
  
      for sock in inputready:
  
          if sock == newSocket:
              (client, (ip, port)) = newSocket.accept()
              input_list.append(client)
              data = client.recv(2048)
              if data:
                  sys.stdout.write(data)
  
          elif sock == sys.stdin:
              msg = sys.stdin.readline()
              newSocket.send('\r&lt;Server&gt;: ' + msg)
              prompt()
  
          else:
              data = sock.recv(2048)
              if data:
                  sys.stdout.write(data)
  
  newSocket.close()
  </code></pre>
  
  <p>client : </p>
  
  <pre><code>import socket
  import os
  import select
  import sys
  
  def prompt():
     sys.stdout.write('Client ')
     sys.stdout.flush()
  
  
  try:
      newSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      newSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  except:
      print 'socket Error'
      sys.exit(1)
  
  
  newSocket.connect(('127.0.0.1', 8000))
  
  print 'Connected to remote host. Start sending messages'
  prompt()
  
  while 1:
  
      socket_list = [sys.stdin, newSocket]
  
      read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])
  
      for sock in read_sockets:
          if sock == newSocket:
              data = sock.recv(4096)
              if not data:
                  print '\nDisconnected from chat server'
                  sys.exit()
              else:
                  sys.stdout.write(data)
                  prompt()
          else:
              msg = sys.stdin.readline()
              newSocket.send('\r&lt;Client&gt;: ' + msg)
              prompt()
  </code></pre>