From 69074960168970979769fa227dfd928189eaf062 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 30 Jun 2016 00:02:45 +0300 Subject: [PATCH] esp8266/websocket_helper.py: Avoid extra string allocations. --- esp8266/scripts/websocket_helper.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/esp8266/scripts/websocket_helper.py b/esp8266/scripts/websocket_helper.py index 22ac28592d..4c388a028b 100644 --- a/esp8266/scripts/websocket_helper.py +++ b/esp8266/scripts/websocket_helper.py @@ -36,21 +36,20 @@ def server_handshake(sock): if DEBUG: print("Sec-WebSocket-Key:", webkey, len(webkey)) - respkey = webkey + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11" - respkey = hashlib.sha1(respkey).digest() + d = hashlib.sha1(webkey) + d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + respkey = d.digest() respkey = binascii.b2a_base64(respkey)[:-1] + if DEBUG: + print("respkey:", resp) - resp = b"""\ + sock.send(b"""\ HTTP/1.1 101 Switching Protocols\r Upgrade: websocket\r Connection: Upgrade\r -Sec-WebSocket-Accept: %s\r -\r -""" % respkey - - if DEBUG: - print(resp) - sock.send(resp) +Sec-WebSocket-Accept: """) + sock.send(respkey) + sock.send("\r\n\r\n") # Very simplified client handshake, works for MicroPython's