title: Python chat client: the server receives commands along with previously sent messages
body: <p>I'm currently working on a project for a class. It consists in code a simple chat client (protocol given by the teacher) to do (at first) some simple tasks.</p>
  
  <p>My problem is that after I send a mensage on the globlal channel or in other channel that doesn't require the use of a command, and try to send any command, the server replies with an error, saying something like: "msgbeforemsgbeforeCOMMAND" is not a valid command. I just cannot figure it out why this is happening...</p>
  
  <p>(<em>another thing, note that my dictionary is not printing the right why, I dont know why to</em>)</p>
  
  <p>ex: 
  <a href="http://i.stack.imgur.com/l9DRH.png" rel="nofollow">chat running</a></p>
  
  <pre><code>import socket, select, string, sys
  import threading
  import time
  
  def prompt():
      sys.stdout.write('&lt;You&gt;: ')
      sys.stdout.flush()
  
  tLock = threading.Lock()
  shutdown = False
  
  def receber(sock):
      while not shutdown:
          try:
              tLock.acquire()
              while True:
                  data = sock.recv(1024)
                  if not data:
                      print ('Disconnected from server\n')
                      sys.exit()
                  else:
                      print ('&lt;server&gt;: %s' % (data.decode()))
                      sys.stdout.write(data)
          except:
              pass
          finally:
              tLock.release()
  
  
  #Main Function
  if __name__ == "__main__":
  
      host = 'mini.alunos.di.uevora.pt'
      port = 143
  
      #IP do servidor
      try:
          busca_ip = socket.gethostbyname( host )
          print ('Chat server IP: %s Port: %d \nNow connecting...\n' %(busca_ip, port))
  
      except socket.gaierror:
          #Não conseguiu o IP do servidor
          print ('Hostname could not be resolved. Exiting.')
          sys.exit()
  
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.settimeout(3)
  
      # connectar ao host
      try :
          s.connect((busca_ip, port))
          s.setblocking(0)
      except :
          print ('Unable to connect to the server.')
          sys.exit()
  
      print ('Connected to chat server. You may start chatting\n')
  
      COM_ARG = {'_Comando_': '_Argumentos_',
                 'NICK': '&lt;nickname&gt; [\&lt;password&gt;]',
                 'MSG': '&lt;recipient&gt; \&lt;message&gt;',
                 'ENTER': '&lt;room&gt;',
                 'LEAVE': '&lt;room&gt; [\&lt;message&gt;]',
                 'RLIST':'',
                 'ULIST':''}
  
      for chave, valor, in COM_ARG.items():
          print (("%s %s") % (chave,valor))
  
      print ('\n')
  
      comandos = COM_ARG.keys()
  
  
      #criar thread para o socket
      t = threading.Thread(target = receber, args=(s,))
      t.start()
  
      while True:
  
          msg = input('&lt;You&gt;: ')
          msg = msg.strip()
          msg12 = msg.upper()
          msg12 = msg12.split()
          try:
              if msg12[0] in comandos:
                  msg = msg + '\n'
          except:
              pass
          s.send(msg.encode())
          time.sleep(0.25)
  </code></pre>
  
  <p>btw, <code>sys.stdout.write(data)</code> is doing something there?</p>
  
  <p>Hope you could help me out.</p>