<p>I have been trying to convert <code>kubectl cp</code> command to it's equivalent <code>kubernetes python client</code> program. I have following code for that:</p>

<pre><code>from kubernetes import client, config
from kubernetes.stream import stream
import tarfile
from tempfile import TemporaryFile

# create an instance of the API class
config.load_kube_config()
api_instance = client.CoreV1Api()

exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(api_instance.connect_get_namespaced_pod_exec, "nginx-deployment-6bb6554bf-9sdtr", 'default',
              command=exec_command,
              stderr=True, stdin=True,
              stdout=True, tty=False,
              _preload_content=False)

source_file = '/tmp/abc.txt'

with TemporaryFile() as tar_buffer:
    with tarfile.open(fileobj=tar_buffer, mode='w') as tar:
        tar.add(source_file)

    tar_buffer.seek(0)
    commands = []
    commands.append(tar_buffer.read())

    while resp.is_open():
        resp.update(timeout=1)
        if resp.peek_stdout():
            print("STDOUT: %s" % resp.read_stdout())
        if resp.peek_stderr():
            print("STDERR: %s" % resp.read_stderr())
        if commands:
            c = commands.pop(0)
            # print("Running command... %s\n" % c)

            resp.write_stdin(c)
        else:
            break
    resp.close()
</code></pre>

<p>The above code gives me following error:</p>

<pre><code>/home/velotio/venv/bin/python /home/velotio/PycharmProjects/k8sClient/testing.py
Traceback (most recent call last):
  File "/home/velotio/PycharmProjects/k8sClient/testing.py", line 38, in &lt;module&gt;
    resp.write_stdin(c)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 160, in write_stdin
    self.write_channel(STDIN_CHANNEL, data)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 114, in write_channel
    self.sock.send(chr(channel) + data)
TypeError: must be str, not bytes
</code></pre>

<p>I am using <code>Python 3.6.3</code> and on <code>kubernetes 1.13</code> version.</p>

<p>You have to convert bytes back to string which is what <code>write_stdin</code> method is expecting to get.</p>

<p>for example:</p>

<pre><code>resp.write_stdin(c.decode())
</code></pre>

<p>Another example:</p>

<pre><code># Array with two byte objects
In [1]: a = [b'1234', b'3455']

# Pop one of them
In [2]: c = a.pop(0)

# it is of byte type
In [3]: c
Out[3]: b'1234'

# wrapping in str won't help if you don't provide decoding
In [4]: str(c)
Out[4]: "b'1234'"

# With decoding
In [5]: str(c, 'utf-8')
Out[5]: '1234'

# Or simply use the decode str method
In [6]: c.decode()
Out[6]: '1234'
</code></pre>

<p>More on byte to string conversion here:
<a href="https://stackoverflow.com/questions/606191/convert-bytes-to-a-string">Convert bytes to a string?</a></p>
