Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 3827074

Browse files
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.
1 parent 4b8e89f commit 3827074

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

stream/ws_client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ def readline_channel(self, channel, timeout=None):
116116

117117
def write_channel(self, channel, data):
118118
"""Write data to a channel."""
119-
self.sock.send(chr(channel) + data)
119+
# check if we're writing binary data or not
120+
binary = six.PY3 and type(data) == six.binary_type
121+
opcode = ABNF.OPCODE_BINARY if binary else ABNF.OPCODE_TEXT
122+
123+
channel_prefix = chr(channel)
124+
if binary:
125+
channel_prefix = six.binary_type(channel_prefix, "ascii")
126+
127+
payload = channel_prefix + data
128+
self.sock.send(payload, opcode=opcode)
120129

121130
def peek_stdout(self, timeout=0):
122131
"""Same as peek_channel with channel=1."""

0 commit comments

Comments
 (0)