2626import six
2727import yaml
2828
29+
2930from six .moves .urllib .parse import urlencode , urlparse , urlunparse
30- from six import StringIO
31+ from six import StringIO , BytesIO
3132
3233from websocket import WebSocket , ABNF , enableTrace
3334from base64 import urlsafe_b64decode
@@ -48,7 +49,7 @@ def getvalue(self):
4849
4950
5051class WSClient :
51- def __init__ (self , configuration , url , headers , capture_all ):
52+ def __init__ (self , configuration , url , headers , capture_all , binary = False ):
5253 """A websocket client with support for channels.
5354
5455 Exec command uses different channels for different streams. for
@@ -58,8 +59,10 @@ def __init__(self, configuration, url, headers, capture_all):
5859 """
5960 self ._connected = False
6061 self ._channels = {}
62+ self .binary = binary
63+ self .newline = '\n ' if not self .binary else b'\n '
6164 if capture_all :
62- self ._all = StringIO ()
65+ self ._all = StringIO () if not self . binary else BytesIO ()
6366 else :
6467 self ._all = _IgnoredIO ()
6568 self .sock = create_websocket (configuration , url , headers )
@@ -92,8 +95,8 @@ def readline_channel(self, channel, timeout=None):
9295 while self .is_open () and time .time () - start < timeout :
9396 if channel in self ._channels :
9497 data = self ._channels [channel ]
95- if " \n " in data :
96- index = data .find (" \n " )
98+ if self . newline in data :
99+ index = data .find (self . newline )
97100 ret = data [:index ]
98101 data = data [index + 1 :]
99102 if data :
@@ -197,10 +200,12 @@ def update(self, timeout=0):
197200 return
198201 elif op_code == ABNF .OPCODE_BINARY or op_code == ABNF .OPCODE_TEXT :
199202 data = frame .data
200- if six .PY3 :
203+ if six .PY3 and not self . binary :
201204 data = data .decode ("utf-8" , "replace" )
202205 if len (data ) > 1 :
203- channel = ord (data [0 ])
206+ channel = data [0 ]
207+ if six .PY3 and not self .binary :
208+ channel = ord (channel )
204209 data = data [1 :]
205210 if data :
206211 if channel in [STDOUT_CHANNEL , STDERR_CHANNEL ]:
@@ -518,13 +523,17 @@ def websocket_call(configuration, _method, url, **kwargs):
518523 _request_timeout = kwargs .get ("_request_timeout" , 60 )
519524 _preload_content = kwargs .get ("_preload_content" , True )
520525 capture_all = kwargs .get ("capture_all" , True )
521-
526+ binary = kwargs . get ( 'binary' , False )
522527 try :
523- client = WSClient (configuration , url , headers , capture_all )
528+ client = WSClient (configuration , url , headers , capture_all , binary = binary )
524529 if not _preload_content :
525530 return client
526531 client .run_forever (timeout = _request_timeout )
527- return WSResponse ('%s' % '' .join (client .read_all ()))
532+ all = client .read_all ()
533+ if binary :
534+ return WSResponse (all )
535+ else :
536+ return WSResponse ('%s' % '' .join (all ))
528537 except (Exception , KeyboardInterrupt , SystemExit ) as e :
529538 raise ApiException (status = 0 , reason = str (e ))
530539
0 commit comments