use socket from exec_run in python3 (python2 is working) -- 'SocketIO' object has no attribute 'sendall'
See original GitHub issueThe following code works fine with pyhton2 (docker-py 2.5.1), but not with python3 (docker-py 3.7.0)
import docker
client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='auto')
container = client.containers.run("gliderlabs/alpine", command= "sleep 100", detach = True)
socket = container.exec_run(cmd="sh", stdin=True, socket = True)
socket.sendall(b"ls\n")
# a read block after a send
try:
unknown_byte=socket.recv(1)
while 1:
# note that os.read does not work
# because it does not TLS-decrypt
# but returns the low-level encrypted data
# one must use "socket.recv" instead
data = socket.recv(16384)
if not data: break
print(data.decode('utf8'))
except so.timeout: pass
socket.sendall(b"exit\n")
in python3 “container.exec_run” returns ExecResult(exit_code=None, output=<socket.SocketIO object at 0x7f5bb7f7c160>).
So my idea was to use socket.output.sendall(b "ls\n")
, but then I always get the error AttributeError: 'SocketIO' object has no attribute 'sendall'""
.
The documentation says only If socket=True, a socket object for the connection
, and sendall() is a socket method for me.
How to use exec_run in python3?
Source: https://github.com/docker/docker-py/issues/983 https://stackoverflow.com/questions/46521166/how-to-write-to-stdin-in-a-tls-enabled-docker-using-the-python-docker-api https://github.com/mailcow/mailcow-dockerized/pull/2297
Version: docker-host: {‘Platform’: {‘Name’: ‘’}, ‘Components’: [{‘Name’: ‘Engine’, ‘Version’: ‘18.03.1-ce’, ‘Details’: {‘ApiVersion’: ‘1.37’, ‘Arch’: ‘amd64’, ‘BuildTime’: ‘2018-04-26T07:15:30.000000000+00:00’, ‘Experimental’: ‘false’, ‘GitCommit’: ‘9ee9f40’, ‘GoVersion’: ‘go1.9.5’, ‘KernelVersion’: ‘4.15.0-45-generic’, ‘MinAPIVersion’: ‘1.12’, ‘Os’: ‘linux’}}], ‘Version’: ‘18.03.1-ce’, ‘ApiVersion’: ‘1.37’, ‘MinAPIVersion’: ‘1.12’, ‘GitCommit’: ‘9ee9f40’, ‘GoVersion’: ‘go1.9.5’, ‘Os’: ‘linux’, ‘Arch’: ‘amd64’, ‘KernelVersion’: ‘4.15.0-45-generic’, ‘BuildTime’: ‘2018-04-26T07:15:30.000000000+00:00’}
conatiner Alpine 3.9 with docker-py 3.7.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:15
Top GitHub Comments
so the above example with python3
methodes for “socket.output._sock”
['__class__', '__del__', '__delattr__', '__dir__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_accept', '_check_sendfile_params', '_decref_socketios', '_real_close', '_sendfile_use_send', '_sendfile_use_sendfile', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'detach', 'dup', 'fileno', 'get_inheritable', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'recvmsg', 'recvmsg_into', 'send', 'sendall', 'sendfile', 'sendmsg', 'sendmsg_afalg', 'sendto', 'set_inheritable', 'setblocking', 'setsockopt', 'settimeout', 'shutdown']
Here’s what I came up with after a day of trying to figure this out:
This creates an ubuntu container that starts up bash. It then attaches stdin, stdout, stderr, in a stream form. I made a little while loop to handle commands. I noticed the first 8 bytes are something special, maybe header data for SocketIO? Not sure. The formatted output strips off those 8 bytes.