Refs. #151 -- detect binary payloads and send the correct opcode

On Python 2, strings are bytestrings either way. On Python 3, the
result of `chr(channel)` is `str`, while the data itself is
`bytes`. The channel prefix needs to be turned into a binary type,
and the websocket frame needs the correct opcode (binary vs. text).

See #151 for the bug report and related issues.
This commit is contained in:
Sergei Maertens 2019-08-14 11:00:17 +02:00
parent 4b8e89f95d
commit 382707436f

View File

@ -116,7 +116,16 @@ class WSClient:
def write_channel(self, channel, data):
"""Write data to a channel."""
self.sock.send(chr(channel) + data)
# check if we're writing binary data or not
binary = six.PY3 and type(data) == six.binary_type
opcode = ABNF.OPCODE_BINARY if binary else ABNF.OPCODE_TEXT
channel_prefix = chr(channel)
if binary:
channel_prefix = six.binary_type(channel_prefix, "ascii")
payload = channel_prefix + data
self.sock.send(payload, opcode=opcode)
def peek_stdout(self, timeout=0):
"""Same as peek_channel with channel=1."""