26
26
import six
27
27
import yaml
28
28
29
+
29
30
from six .moves .urllib .parse import urlencode , urlparse , urlunparse
30
- from six import StringIO
31
+ from six import StringIO , BytesIO
31
32
32
33
from websocket import WebSocket , ABNF , enableTrace
33
34
from base64 import urlsafe_b64decode
@@ -48,7 +49,7 @@ def getvalue(self):
48
49
49
50
50
51
class WSClient :
51
- def __init__ (self , configuration , url , headers , capture_all ):
52
+ def __init__ (self , configuration , url , headers , capture_all , binary = False ):
52
53
"""A websocket client with support for channels.
53
54
54
55
Exec command uses different channels for different streams. for
@@ -58,8 +59,10 @@ def __init__(self, configuration, url, headers, capture_all):
58
59
"""
59
60
self ._connected = False
60
61
self ._channels = {}
62
+ self .binary = binary
63
+ self .newline = '\n ' if not self .binary else b'\n '
61
64
if capture_all :
62
- self ._all = StringIO ()
65
+ self ._all = StringIO () if not self . binary else BytesIO ()
63
66
else :
64
67
self ._all = _IgnoredIO ()
65
68
self .sock = create_websocket (configuration , url , headers )
@@ -92,8 +95,8 @@ def readline_channel(self, channel, timeout=None):
92
95
while self .is_open () and time .time () - start < timeout :
93
96
if channel in self ._channels :
94
97
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 )
97
100
ret = data [:index ]
98
101
data = data [index + 1 :]
99
102
if data :
@@ -197,10 +200,12 @@ def update(self, timeout=0):
197
200
return
198
201
elif op_code == ABNF .OPCODE_BINARY or op_code == ABNF .OPCODE_TEXT :
199
202
data = frame .data
200
- if six .PY3 :
203
+ if six .PY3 and not self . binary :
201
204
data = data .decode ("utf-8" , "replace" )
202
205
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 )
204
209
data = data [1 :]
205
210
if data :
206
211
if channel in [STDOUT_CHANNEL , STDERR_CHANNEL ]:
@@ -518,13 +523,17 @@ def websocket_call(configuration, _method, url, **kwargs):
518
523
_request_timeout = kwargs .get ("_request_timeout" , 60 )
519
524
_preload_content = kwargs .get ("_preload_content" , True )
520
525
capture_all = kwargs .get ("capture_all" , True )
521
-
526
+ binary = kwargs . get ( 'binary' , False )
522
527
try :
523
- client = WSClient (configuration , url , headers , capture_all )
528
+ client = WSClient (configuration , url , headers , capture_all , binary = binary )
524
529
if not _preload_content :
525
530
return client
526
531
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 ))
528
537
except (Exception , KeyboardInterrupt , SystemExit ) as e :
529
538
raise ApiException (status = 0 , reason = str (e ))
530
539
0 commit comments