From aff660463695829a17d3ae4d4afe88d7e5bcf5c5 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Wed, 16 Jun 2021 20:49:50 +0200 Subject: [PATCH] Upgrade websockets dependency. (#2154) * Upgrade websockets dependency. Fix #2142. * Bumpt sanic-testing version Co-authored-by: Adam Hopkins Co-authored-by: Adam Hopkins --- sanic/websocket.py | 10 ++++++++-- setup.py | 4 ++-- tox.ini | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sanic/websocket.py b/sanic/websocket.py index 6b325e26..c4bdd580 100644 --- a/sanic/websocket.py +++ b/sanic/websocket.py @@ -14,9 +14,13 @@ from websockets import ( # type: ignore ConnectionClosed, InvalidHandshake, WebSocketCommonProtocol, - handshake, ) +# Despite the "legacy" namespace, the primary maintainer of websockets +# committed to maintaining backwards-compatibility until 2026 and will +# consider extending it if sanic continues depending on this module. +from websockets.legacy import handshake + from sanic.exceptions import InvalidUsage from sanic.server import HttpProtocol @@ -126,7 +130,9 @@ class WebSocketProtocol(HttpProtocol): ping_interval=self.websocket_ping_interval, ping_timeout=self.websocket_ping_timeout, ) - # Following two lines are required for websockets 8.x + # we use WebSocketCommonProtocol because we don't want the handshake + # logic from WebSocketServerProtocol; however, we must tell it that + # we're running on the server side self.websocket.is_client = False self.websocket.side = "server" self.websocket.subprotocol = subprotocol diff --git a/setup.py b/setup.py index 3a43b11e..0a9ddbfb 100644 --- a/setup.py +++ b/setup.py @@ -88,12 +88,12 @@ requirements = [ uvloop, ujson, "aiofiles>=0.6.0", - "websockets>=8.1,<9.0", + "websockets>=9.0", "multidict>=5.0,<6.0", ] tests_require = [ - "sanic-testing", + "sanic-testing>=0.6.0", "pytest==5.2.1", "multidict>=5.0,<6.0", "gunicorn==20.0.4", diff --git a/tox.ini b/tox.ini index 25b29346..dac8dacb 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,7 @@ setenv = {py37,py38,py39,pyNightly}-no-ext: SANIC_NO_UJSON=1 {py37,py38,py39,pyNightly}-no-ext: SANIC_NO_UVLOOP=1 deps = - sanic-testing + sanic-testing>=0.6.0 coverage==5.3 pytest==5.2.1 pytest-cov @@ -18,7 +18,7 @@ deps = beautifulsoup4 gunicorn==20.0.4 uvicorn - websockets>=8.1,<9.0 + websockets>=9.0 commands = pytest {posargs:tests --cov sanic} - coverage combine --append