title: Why does my output vary when sending lists over a TCP socket in Python 3.6?
body: <p>Could someone please explain why my socket program outputs the message in a strange way everytime I run the code.</p>
  
  <p>I am supposed to receive the server results from a list sent from the client as follows:</p>
  
  <blockquote>
    <p>Sending request 'E1.0 -945 1689 -950 230 -25 1'</p>
    
    <p>Received response: </p>
    
    <p>Sending request 'S0 2 -945 1689 -950 230 -25 1 1e-15'</p>
    
    <p>Received response: </p>
    
    <p>...</p>
  </blockquote>
  
  <p>Instead, I get this: </p>
  
  <blockquote>
    <p>Sending request 'E1.0 -945 1689 -950 230 -25 1'</p>
    
    <p>Received response: </p>
    
    <p>Sending request 'S0 2 -945 1689 -950 230 -25 1 1e-15'</p>
    
    <p>Received response: </p>
    
    <p>Sending request 'G4.1 0 0'Received response: XIncorrect command type</p>
    
    <p>Sending request '4 1 0'</p>
    
    <p>Received response: XIncorrect command type</p>
    
    <p>Sending request 'E1.0'</p>
    
    <p>Received response: </p>
    
    <p>Sending request 'S1.0'Received response: </p>
    
    <p>Sending request 'S0 2 -945 1689 -950 230 -25 1 -1e-15'Received response: </p>
    
    <p>Sending request 'Not a number'Received response: XIncorrect command type</p>
  </blockquote>
  
  <p>This happens all the time, sometimes it works as it should most time the received data is out of place. This is my code...</p>
  
  <p>server.py</p>
  
  <pre><code>import socket
  port = 12345
  mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  mySocket.bind(('localhost', port))
  mySocket.listen(0)
  clientsocket, addr = mySocket.accept()
  print("Connection from: {}\n".format(str(addr)))
  
  while True:
      encoded_message = clientsocket.recv(2048)
      data = encoded_message.decode()
      print(data)
      if not data:
          break
      else:
          acknowledge = "Sending request \'{}\'".format(data)
          clientsocket.sendall(acknowledge.encode())
          request_code = data[0]
          if request_code == 'E':
              messageO = "Received response: \n\r"
              clientsocket.sendall(messageO.encode())
          elif request_code == 'S':
              messageO = "Received response: \n\r"
              clientsocket.sendall(messageO.encode())
          else:
              messageO = "Received response: XIncorrect command type\n\r"
              clientsocket.sendall(messageO.encode())
  
  clientsocket.close()
  </code></pre>
  
  <p>client.py</p>
  
  <pre><code>import socket
  host = 'localhost'
  port = 12345
  
  mySocket = socket.socket()
  mySocket.connect((host, port))
  
  testing_strings = ["E1.0 -945 1689 -950 230 -25 1",
                     "S0 2 -945 1689 -950 230 -25 1 1e-15",
                     "G4.1 0 0",
                     "4 1 0",
                     "E1.0",
                     "S1.0",
                     "S0 2 -945 1689 -950 230 -25 1 -1e-15",
                     "Not a number",
                     "S0 2 -945 1689 -950 230 -25 1 0",
                     "S0 2 -945 1689 -950 230 G 1 1e-15"]
  
  count = 0
  print(len(testing_strings))
  count = 0
  for l in testing_strings:
      if count == (len(testing_strings)-1):
          mySocket.send(l.encode())
          data = mySocket.recv(2048).decode('utf-8')
          print(data)
          mySocket.shutdown(1)
      else:
          mySocket.send(l.encode())
          data = mySocket.recv(2048).decode('utf-8')
          print(data)
          count += 1
  
  mySocket.close()
  </code></pre>