esp8266/websocket_helper.py: Avoid extra string allocations.

This commit is contained in:
Paul Sokolovsky 2016-06-30 00:02:45 +03:00
parent f3636a7b46
commit 6907496016

View File

@ -36,21 +36,20 @@ def server_handshake(sock):
if DEBUG: if DEBUG:
print("Sec-WebSocket-Key:", webkey, len(webkey)) print("Sec-WebSocket-Key:", webkey, len(webkey))
respkey = webkey + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11" d = hashlib.sha1(webkey)
respkey = hashlib.sha1(respkey).digest() d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
respkey = d.digest()
respkey = binascii.b2a_base64(respkey)[:-1] respkey = binascii.b2a_base64(respkey)[:-1]
if DEBUG:
print("respkey:", resp)
resp = b"""\ sock.send(b"""\
HTTP/1.1 101 Switching Protocols\r HTTP/1.1 101 Switching Protocols\r
Upgrade: websocket\r Upgrade: websocket\r
Connection: Upgrade\r Connection: Upgrade\r
Sec-WebSocket-Accept: %s\r Sec-WebSocket-Accept: """)
\r sock.send(respkey)
""" % respkey sock.send("\r\n\r\n")
if DEBUG:
print(resp)
sock.send(resp)
# Very simplified client handshake, works for MicroPython's # Very simplified client handshake, works for MicroPython's