title: python sockets can't send multiple messages -- data is referenced before assignment--
body: <p>Hi i'm trying to send multiple messages to the tcp server but in my client i got an error that data is referenced before assignment. If i send one message there will be no error but if i try to send more than one it returns the error. </p>
  
  <p>tcp server: </p>
  
  <pre><code>class Connect(object):
  
      def __init__(self):
          try:
              self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          except:
              print('socket cannot be created')
          server_address = ('169.254.34.240', 10000)
          #print('starting up: ' + server_address)
          self.sock.bind(server_address)
          self.sock.listen(1)
  
      def listen(self):
          while True:
              connection, client_address = self.sock.accept()
              print('client connected')
              try:
                  data = connection.recv(16)
                  print(data)
                  if data == "STATUS":
                      connection.sendall("vision=ready")
                  elif data == "MEASURE":
                      connection.sendall("vision=computing")
                  elif data == "GET_RESULT":
                      connection.sendall("x=1.5,y=0.25,z=0.14,a=0.15")
                  else:
                      connection.sendall("wrong command")
              finally:
                  connection.close()
  
  
  def main():
      connect = Connect()
      connect.listen()
  
  if __name__=='__main__':
      main() 
  </code></pre>
  
  <p>my tcp client which is sending messages:</p>
  
  <pre><code>class Connect(object):
  
      def __init__(self): 
          # Create a TCP/IP socket
          self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
          # Connect the socket to the port on the server given by the caller
          print('connecting to host')
          self.sock.connect(('169.254.34.240',10000))
  
      def send(self, command):
          try:
              message = command
              print('sending: ' + message)
              self.sock.sendall(message)
              amount_received = 0
                  amount_expected = len(message)
                  while amount_received &lt; amount_expected:
                      data = self.sock.recv(16)
                      amount_received += len(data)
                      print('received: ' + data)
          finally:
              self.sock.close()
              return data
  
  def main():
      connect = Connect()
      print connect.send("STATUS")
      print connect.send("MEASURE")
  
  if __name__=='__main__':
      main()
  </code></pre>
  
  <p>so anyone an idea, i suppose i don't end correctly or something, i thought it had something to do about my while in the client? </p>