Implement attach_websocket() for attaching with WebSockets instead of HTTP streaming

This commit is contained in:
Aanand Prasad 2013-09-04 22:27:51 -04:00
parent 3754edc267
commit c2d867b117
2 changed files with 18 additions and 8 deletions

View File

@ -24,6 +24,8 @@ import auth
import unixconn
import utils
import websocket
class APIError(requests.exceptions.HTTPError):
def __init__(self, message, response, explanation=None):
@ -139,23 +141,23 @@ class Client(requests.Session):
return self.post(url, json.dumps(data2), **kwargs)
def attach_socket(self, container, params=None):
if params is None:
params = {
'stdout': 1,
'stderr': 1,
'stream': 1
}
if isinstance(container, dict):
container = container.get('Id')
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)
# hijack the underlying socket from requests, icky
# but for some reason requests.iter_contents and ilk
# eventually block
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):
socket = self.attach_socket(container)
@ -166,6 +168,13 @@ class Client(requests.Session):
else:
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):
remote = context = headers = None
if path is None and fileobj is None:

View File

@ -1,2 +1,3 @@
requests==1.2.0
six==1.3.0
websocket-client==0.11.0