Simulating a shell via socket connection - Python
<p><strong>No, this is not part of a homework</strong></p>

<p>I'm working on a project where I need to simulate <code>shell</code> commands executed in a remote docker container. I'm able to obtain the connection to the container, which is an <code>OpenSSL</code> connection and a <code>socket</code>. 
When I execute commands like <code>command="ls"</code> then I'm able to read the output with <code>read()</code>:</p>

<pre><code>def read(self, socket, n=4096):
    """
    Return `n` bytes of data from the Stream, or None at the end of the stream

    :param n:
    :return:
    """
    while True:
        try:
            if hasattr(socket, 'recv'):
                return socket.recv(n)
            return os.read(socket.fileno(), n)
        except Exception, e:
            print e
</code></pre>

<p>so for example:</p>

<pre><code>container = self.client.create_container()
exec_id = self.client.exec_create(container, command, tty=True)
socket = self.client.exec_start(exec_id=exec_id, tty=True, stream=True)
response = self.read(socket)
</code></pre>

<p>I can get the following output:</p>

<pre><code>bin       openssl
etc       share
include
lib       
license-eula.txt  var
</code></pre>

<p>However, things get more interesting when executing commands such as <code>/bin/bash</code>. So, in this case, I should keep the interacting with the connection, getting input from <code>stdin</code>, writing and reading from the socket...  I do have a way to resize the terminal, so that's not my biggest concern. My question is: How can I simulate a terminal via the socket connection? I do know about <code>dockerpty</code> but it won't work on my setup due to a <code>tls</code> connection (<a href="https://stackoverflow.com/questions/39709781/python-tlsv1-alert-protocol-version-error-in-docker-client-connection/39715026#39715026">Here's the ticket I created</a>). </p>