mirror of https://github.com/docker/docker-py.git
Implement attach_websocket() for attaching with WebSockets instead of HTTP streaming
This commit is contained in:
parent
3754edc267
commit
c2d867b117
|
|
@ -24,6 +24,8 @@ import auth
|
||||||
import unixconn
|
import unixconn
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
import websocket
|
||||||
|
|
||||||
|
|
||||||
class APIError(requests.exceptions.HTTPError):
|
class APIError(requests.exceptions.HTTPError):
|
||||||
def __init__(self, message, response, explanation=None):
|
def __init__(self, message, response, explanation=None):
|
||||||
|
|
@ -139,23 +141,23 @@ class Client(requests.Session):
|
||||||
return self.post(url, json.dumps(data2), **kwargs)
|
return self.post(url, json.dumps(data2), **kwargs)
|
||||||
|
|
||||||
def attach_socket(self, container, params=None):
|
def attach_socket(self, container, params=None):
|
||||||
if params is None:
|
|
||||||
params = {
|
|
||||||
'stdout': 1,
|
|
||||||
'stderr': 1,
|
|
||||||
'stream': 1
|
|
||||||
}
|
|
||||||
if isinstance(container, dict):
|
if isinstance(container, dict):
|
||||||
container = container.get('Id')
|
container = container.get('Id')
|
||||||
|
|
||||||
u = self._url("/containers/{0}/attach".format(container))
|
u = self._url("/containers/{0}/attach".format(container))
|
||||||
res = self.post(u, None, params=params, stream=True)
|
res = self.post(u, None, params=self._attach_params(params), stream=True)
|
||||||
self._raise_for_status(res)
|
self._raise_for_status(res)
|
||||||
# hijack the underlying socket from requests, icky
|
# hijack the underlying socket from requests, icky
|
||||||
# but for some reason requests.iter_contents and ilk
|
# but for some reason requests.iter_contents and ilk
|
||||||
# eventually block
|
# eventually block
|
||||||
return res.raw._fp.fp._sock
|
return res.raw._fp.fp._sock
|
||||||
|
|
||||||
|
def attach_websocket(self, container, params=None):
|
||||||
|
url = self._url("/containers/{0}/attach/ws".format(container))
|
||||||
|
req = requests.Request("POST", url, params=self._attach_params(params))
|
||||||
|
full_url = req.prepare().url.replace("http://", "ws://", 1)
|
||||||
|
print full_url
|
||||||
|
return websocket.create_connection(full_url)
|
||||||
|
|
||||||
def attach(self, container):
|
def attach(self, container):
|
||||||
socket = self.attach_socket(container)
|
socket = self.attach_socket(container)
|
||||||
|
|
||||||
|
|
@ -166,6 +168,13 @@ class Client(requests.Session):
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def _attach_params(self, override=None):
|
||||||
|
return override or {
|
||||||
|
'stdout': 1,
|
||||||
|
'stderr': 1,
|
||||||
|
'stream': 1
|
||||||
|
}
|
||||||
|
|
||||||
def build(self, path=None, tag=None, quiet=False, fileobj=None, nocache=False):
|
def build(self, path=None, tag=None, quiet=False, fileobj=None, nocache=False):
|
||||||
remote = context = headers = None
|
remote = context = headers = None
|
||||||
if path is None and fileobj is None:
|
if path is None and fileobj is None:
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
requests==1.2.0
|
requests==1.2.0
|
||||||
six==1.3.0
|
six==1.3.0
|
||||||
|
websocket-client==0.11.0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue