title: How to fix: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 18: invalid start byte
body: <p>I am trying to make a remote shell, using sockets. Some commands are working like <code>netstat</code> or <code>dir</code>. But when I try to use a <code>ping</code>, this error message comes up:</p>
  <ul>
  <li>UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 18: invalid start byte</li>
  </ul>
  <p>This problem only occurs on the server side, if I try to encode the message in utf-8. With a normal byte string it works fine. I have also tried to receive first and then encode the string.</p>
  <p>Server side:</p>
  <pre class="lang-py prettyprint-override"><code>    while True:
          cmd = input(&quot;&gt;&gt;&gt;&quot;)
  
          if len(str.encode(cmd)) &gt; 0:
              connection.send(str.encode(cmd))
  
              client_response = str(connection.recv(1024), encoding = &quot;utf-8&quot;)
  
              print (client_response, end = &quot;&quot;)
  </code></pre>
  <p>Client side:</p>
  <pre class="lang-py prettyprint-override"><code>    while True:
          data = sock.recv(1024).decode(&quot;utf-8&quot;)
  
          proc = subprocess.Popen(shlex.split(data),
                                  shell = True,
                                  stdin = subprocess.PIPE,
                                  stdout = subprocess.PIPE,
                                  stderr = subprocess.PIPE)
  
          output = proc.stdout.read()+proc.stderr.read()
  
          sock.send(output)
  </code></pre>
  <p>Maybe someone could give me a tip.</p>