question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

use socket from exec_run in python3 (python2 is working) -- 'SocketIO' object has no attribute 'sendall'

See original GitHub issue

The 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:open
  • Created 5 years ago
  • Reactions:3
  • Comments:15

github_iconTop GitHub Comments

2reactions
christianburcommented, Feb 22, 2019

so the above example with python3

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)
print(socket)
socket.output._sock.send(b"ls\n")

#print socket
#socket.sendall(b"ls\n")

# a read block after a send
try:
    unknown_byte=socket.output._sock.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.output._sock.recv(16384)
        if not data: break
        print(data.decode('utf8'))
except so.timeout: pass

socket.output._sock.send(b"exit\n")

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']

2reactions
jrcichracommented, Feb 21, 2019

Here’s what I came up with after a day of trying to figure this out:

import docker, tarfile
from io import BytesIO

client = docker.APIClient()

# create container
container = client.create_container(
    'ubuntu',
    stdin_open = True,
    # environment=["PS1=#"],
    command    = 'bash')
client.start(container)

# attach stdin to container and send data
s = client.attach_socket(container, params={'stdin': 1, 'stream': 1,'stdout':1,'stderr':1})

while True:
    original_text_to_send = input("$") + '\n'
    if(original_text_to_send == "exit\n"):
        s.close()
        break
    else:
        s._sock.send(original_text_to_send.encode('utf-8'))
        msg = s._sock.recv(1024)
        print(msg)
        print(len(msg))
        print('==================')
        print(msg.decode()[8:])


print("We're done here")
client.stop(container)
client.wait(container)
client.remove_container(container)

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

use socket from exec_run in python3 (python2 is working)
use socket from exec_run in python3 (python2 is working) -- 'SocketIO' object has no attribute 'sendall'
Read more >
'module' object has no attribute 'send'" When using socket for ...
Your program is using the socket module as though it were a connected socket but it isn't. It's just a module where socket-related...
Read more >
AttributeError: 'NoneType' object has no attribute 'sendall'
Hi , I have requirement to simulate http server on particular port we ... code, message)) File "/usr/lib64/python2.6/socket.py", line 324, ...
Read more >
The Socket.IO Server — python-socketio documentation
The Socket.IO Server¶. This package contains two Socket.IO servers: The socketio.Server() class creates a server compatible with the Python standard library ...
Read more >
socket module — MicroPython latest documentation
In CPython, you need to convert a socket to a file-like object using makefile() ... of memory and processing power) and portable way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found