title: Is there any way to avoid missing bytes on a Python socket data transmission?
body: <p>I have two different python clients communicating with each other (Client A and Client B) through an external node server on the internet. Client A should send many identified messages in a short period of time to client B, where the node server is responsible to forward all received messages from A to B. </p>
  
  <p>What is happening is that client A is overwriting some message. </p>
  
  <p>I have detected the problem on client B, which is where I analyze the messages. However, the problem occurs in the transmission of messages between client A and Node server, because I am already able to see the broken message on node server logs.</p>
  
  <p>I will show some pseudo-code along with some logs regarding the error (Client A and Server only) :</p>
  
  <p><b>Client A</b></p>
  
  <p>Code
      ...
      import socket</p>
  
  <pre><code>server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_socket.connect((HOST, PORT))
  
  ...
  
  while not END:
      #Prepare message_to_clinet
  
      #If I uncomment this timer, the error will not appear (or appear later)
      #time.sleep(0.2)
  
      # I have tried either sendall and send. But it seems that is not effective
      server_socket.sendall(message_to_client)
      print '(Sent):' + message_to_client
  </code></pre>
  
  <p>Logs when the error occurs
  Imagine that I want to send 100 times the message: 'Where is the bug?'</p>
  
  <pre><code>...
  (Sent): Where is the bug?
  (Sent): Where is the bug?
  (Sent): Where is the bug?
  (Sent): Where is the bug?
  (Sent): Where is the bug?
  ...
  </code></pre>
  
  <p><b>Node</b></p>
  
  <p>Code</p>
  
  <pre><code>const net = require('net');
  const tcpServer = net.createServer(connection.requestHandler);
  tcpServer.listen(connection.port, (err) =&gt; {
      if (err) {
          return console.log('tcp socket error', err);
      }
  
      console.log(`tcp socket listening on ${connection.port}`);
  });
  
  ...
  
  const requestHandler = (sock) =&gt; {   
      sock.on('data', (data) =&gt; {
          const protocol_message = new Buffer(data).toString('ascii');
  
          //Send this message to remote client
          currentConnection.sendMessagesToClient(protocol_message)
          console.log('Received: ' + protocol_message)
      }
  }
  </code></pre>
  
  <p>Logs when the error occurs.
  I receive more messages for each socket.send maybe because socket.send it is called several times in a short time on client A (which is not a problem because on the real code I have a 4 byte-length prefix to prevent errors when reading on clinet B)</p>
  
  <pre><code>...
  Received: Where is the bug? Where is the bug? Where is the bug?  Where is the bug?Where is the bug?
  Received: Where is the bug? Where is the bug? Where is the bug?  Where is the bug?Where is the bug?
  Received: Where is thWhere is the bug? Where is the bug?Where is the bug?
  ...
  </code></pre>
  
  <p>Here I can see that one of the 100 messages I repeatedly sent is broken. However, on client A, everything is apparently well.</p>
  
  <p>Does anyone know what is a possible cause of this error? </p>
  
  <p>I know if put a time.sleep(0.1) between each message on client A, the error takes more time and transmitted bytes to happen. But when it works, it is just too slow. 
  I also know that if I use the developer local server, I cannot reproduce the error. My guess is that this is related to socket buffer and delay on transmission.</p>
  
  <p>EDIT: I am using Python 2.7</p>