title: how to calculate data from socket in server and then return results python
body: <p>I'm a newbie in python and I'm stuck in socket problem. So I have an array of string and I want to get the sum of each item. Below is my code and my expected results but I keep getting errors. I would appreciate any help. Thanks in advance.  </p>
  
  <p>Client.py:</p>
  
  <pre><code>import socket
  
  sock = socket.socket()
  sock.connect(("localhost",31415))
  
  testing_strings = ["1 2 3", "4 5 6", "1 2 5"]
  
  
  for c in testing_strings:
      encoded_message = c.encode()
      sock.sendall(encoded_message)
  
  sock.shutdown(1)
  
  message = ""
  
  encoded_message = sock.recv(2048)
  while len(encoded_message) &gt; 0:
      message += "result is: " + encoded_message.decode() + "\n"
      encoded_message = sock.recv(2048)
  
  print(message)
  sock.close()
  </code></pre>
  
  <p>Server.py:</p>
  
  <pre><code>import socket
  
  listener = socket.socket()
  listener.bind(("",31415))
  listener.listen()
  
  while 1:
      conn = listener.accept()
      sock = conn[0]
  
      message  = ""
      sum =0
      encoded_message = sock.recv(2048)
      while len(encoded_message) &gt; 0:
          message += encoded_message.decode()
          y = [float(x) for x in message.split(' ')]
          for x in y:
              sum+= x
  
          encoded_message = sock.recv(2048)
  
          sock.sendall(sum.encode())
          sock.close()
  </code></pre>
  
  <p>The expected output would be </p>
  
  <pre><code>result is: 6
  result is: 15
  result is: 8
  </code></pre>