Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix byte/string conversion for Python 3. #9

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from io import StringIO
from http.server import SimpleHTTPRequestHandler
from urllib.parse import urlsplit
b2s = lambda buf: buf.decode('latin_1')
s2b = lambda s: s.encode('latin_1')
b2s = lambda buf: buf.decode('latin_1') if isinstance(buf, bytes) else buf
s2b = lambda s: s.encode('latin_1') if isinstance(s, str) else s
else:
# python 2.X
from cStringIO import StringIO
Expand Down Expand Up @@ -312,7 +312,7 @@ def decode_hybi(buf, base64=False):
offset=full_len - (f['length'] % 4),
count=(f['length'] % 4))
c = numpy.bitwise_xor(data, mask).tostring()
f['payload'] = b + c
f['payload'] = s2b(b) + s2b(c)
else:
print("Unmasked frame: %s" % repr(buf))
f['payload'] = buf[(f['hlen'] + has_mask * 4):full_len]
Expand Down Expand Up @@ -635,7 +635,7 @@ def do_handshake(self, sock, address):
# Generate the hash value for the accept header
accept = b64encode(sha1(s2b(key + self.GUID)).digest())

response = self.server_handshake_hybi % accept
response = self.server_handshake_hybi % b2s(accept)
if self.base64:
response += "Sec-WebSocket-Protocol: base64\r\n"
else:
Expand Down