title: socket-based simple python chat program
body: <p>I am writing a simple socket-based chat program that allows the server to send and receive message to the client. The client can send message to server but when I try sending a message from the server, it crashes saying that 'file' objects has attribute 'recv'.</p>
  
  <h1>Server.py</h1>
  
  <pre><code>import socket
  import os
  import select
  import sys
  
  def prompt():
      sys.stdout.write('&lt;You&gt; ')
      sys.stdout.flush()
  
  try:
      server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  except:
      print 'Failed to create socket'
      sys.exit()
  
  PORT = 9050
  HOST = '127.0.0.1'
  RECV_BUFFER = 4096
  
  server_socket.bind((HOST, PORT))
  server_socket.listen(10)
  
  input = [server_socket, sys.stdin]
  
  print 'Chat Program'
  prompt()
  
  while 1:
  
      inputready, outputready, exceptready = select.select(input,[],[])
  
      for sock in inputready:
  
          if sock == server_socket:
              client, address = server_socket.accept()
              input.append(client)
              #data = sock.recv(RECV_BUFFER)
              #if data:
                  #sys.stdout.write(data)
          else:
              data = sock.recv(RECV_BUFFER)
              if data:
                  sys.stdout.write(data)
              else:
                  msg = sys.stdin.readline()
                  server_socket.send('\r&lt;Server&gt;: ' + msg)
                  prompt()
  
  
  
  server_socket.close()
  </code></pre>
  
  <h1>Client.py</h1>
  
  <pre><code>import socket
  import os
  import select
  import sys
  
  def prompt():
     sys.stdout.write('&lt;You&gt; ')
     sys.stdout.flush()
  
  HOST = '127.0.0.1'
  PORT = 9050
  try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  except socket.error:
      print 'Failed to create socket'
      sys.exit()
  
  s.connect((HOST, PORT))
  
  print 'Connected to remote host. Start sending messages'
  prompt()
  
  while 1:
  
      socket_list = [sys.stdin, s]
  
      read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])
  
      for sock in read_sockets:
          if sock == s:
              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()
              s.send('\r&lt;Client&gt;: ' + msg)
              prompt()
  </code></pre>