Skip to content

Commit 4d5bc1e

Browse files
committed
1 parent 8a73907 commit 4d5bc1e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ChangeLog
55
- 0.34.0
66

77
- Change import style (#203)
8+
- fix attribute error on the older python. (#215)
89

910
- 0.33.0
1011

websocket/_handshake.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040

4141
__all__ = ["handshake_response", "handshake"]
4242

43+
if hasattr(hmac, "compare_digest"):
44+
compare_digest = hmac.compare_digest
45+
else:
46+
def compare_digest(s1, s2):
47+
return s1 == s2
48+
4349
# websocket supported version.
4450
VERSION = 13
4551

@@ -99,7 +105,7 @@ def _get_handshake_headers(resource, host, port, options):
99105
if isinstance(header, dict):
100106
header = map(": ".join, header.items())
101107
headers.extend(header)
102-
108+
103109
cookie = options.get("cookie", None)
104110

105111
if cookie:
@@ -149,10 +155,7 @@ def _validate(headers, key, subprotocols):
149155

150156
value = (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode('utf-8')
151157
hashed = base64encode(hashlib.sha1(value).digest()).strip().lower()
152-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
153-
success = (hashed == result)
154-
else:
155-
success = hmac.compare_digest(hashed, result)
158+
success = compare_digest(hashed, result)
156159

157160
if success:
158161
return True, subproto

0 commit comments

Comments
 (0)