title: Socket Programming trying to send a string and reverse it and then receive that reversed string
body: <p>I'm working on a code file in which I need to send a string, have that data received and then the string is reversed and sent back in that reversed format. My current code is not reaching that reverse point and I am unsure of why or how to figure out why it is not reaching that point at all. This is what I got:</p>
  <pre><code>import argparse, socket, sys, threading
  
  try:
      import SocketServer as socketserver
  except:
      import socketserver
  
  from threading import Thread
  
  try:
      input_function = raw_input
  except:
      input_function = input
  
  class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
  
      def handle(self):
          data = self.request.recv(1024)
          cur_thread = threading.currentThread()
          response = &quot;%s: %s&quot; % (cur_thread.getName(), data)
          self.request.send(response.encode())
  
  class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
      pass
  
  def client(ip, port, message):
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect((ip, port))
      encoded_message = message.encode()
      sock.sendall(encoded_message)
      response = sock.recv(1024)
      print(&quot;Received: %s&quot; % response)
      sock.close()
  
  # main
  if __name__ == '__main__':
      parser = argparse.ArgumentParser(description='Send and receive TCP')
      parser.add_argument('-ip_address', help='ip address', default=&quot;127.0.0.1&quot;)
      parser.add_argument('-p', metavar='PORT', type=int, default=1060,
              help='TCP port (default 1060)')
      args = parser.parse_args()
      addr = args.ip_address
      port = args.p
      &quot;&quot;&quot;
          The IP address passed in will be the IP address that the client will connect to.
          It has been set to default to the local loopback address of 127.0.0.1 for testing purposes
  
          The port number will be both the port that the server will listen on, and the port that the client will try to connect to.
      &quot;&quot;&quot;
      # set up threads for both sending and receiving
      try:
          server = ThreadedTCPServer((addr, port), ThreadedTCPRequestHandler)
          # Start a thread with the server -- that thread will then start one
          # more thread for each request
          server_thread = threading.Thread(target=server.serve_forever)
          # Exit the server thread when the main thread terminates
          server_thread.setDaemon(True)
          server_thread.start()
          print( &quot;Server loop running in thread:&quot;+ server_thread.getName() )
      except:
          # assume it couldn't work because we want to try this script
          # as multiple clients connecting to the same server
          # in this case, the server will have already bound to the port
          # and we can't expect to reuse the port
          print(&quot;Server is already running on this port&quot;)
          # but we keep on running so that the client can still work
  
      # Now we have the client connect to the server
      prompt_string = &quot;What data would you like to send to the server?&quot;
      t = input_function(prompt_string)
      while (len(t) &gt; 0):
          client(addr,port,t)
          t = input_function(prompt_string)
  
      try:
          server.shutdown()
      except:
          pass
  
  </code></pre>
  <p>and this is the reverse string code:</p>
  <pre><code>import socket, sys
  
  def reverse_string_as_data(s):
      return str(&quot;&quot;.join(list(reversed(s.decode(&quot;utf-8&quot;))))).encode()
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #@ create a new TCP socket and assign it to the variable sock
  port = int(sys.argv[1])
  sock.bind(('', port)) #@ bind the sock to the default IP address on the port specified in the variable port
  sock.listen(1) #@ tell sock to queue up at most one waiting connection through the use of listen
  while True:
      conn, (addr, port) = sock.accept() #@ conn, (addr, port) should be the result of calling the accept method of sock
      data = conn.recv(1024) #@ receive some data on conn and assign it to data (note that recv returns a tuple - we only care about part and can put the rest in a dummy variable)
      atad = reverse_string_as_data(data)
      conn.sendall(atad) #@ send all of atad on conn
      conn.close() #@ invoke the close method on conn
  </code></pre>
  <p>right now all I get in response is the string I type with a b in front of it, and I am stuck on the book I have been basing this off not being enough help.</p>