From 0d6bf528446bbd8cca996cf6cdb1fa6c7790309c Mon Sep 17 00:00:00 2001 From: JiayuXu <84259897+JiayuXu0@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:22:12 +0800 Subject: [PATCH] fix: support both old and new websockets request headers format (#1588) Co-authored-by: xujiayu --- python-api-examples/non_streaming_server.py | 6 +++++- python-api-examples/streaming_server.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/python-api-examples/non_streaming_server.py b/python-api-examples/non_streaming_server.py index 3dd12564..3ffa8b7d 100755 --- a/python-api-examples/non_streaming_server.py +++ b/python-api-examples/non_streaming_server.py @@ -641,7 +641,11 @@ class NonStreamingServer: path: str, request_headers: websockets.Headers, ) -> Optional[Tuple[http.HTTPStatus, websockets.Headers, bytes]]: - if "sec-websocket-key" not in request_headers: + if "sec-websocket-key" not in ( + request_headers.headers # For new request_headers + if hasattr(request_headers, "headers") + else request_headers # For old request_headers + ): # This is a normal HTTP request if path == "/": path = "/index.html" diff --git a/python-api-examples/streaming_server.py b/python-api-examples/streaming_server.py index c7c1b8de..5691f67c 100755 --- a/python-api-examples/streaming_server.py +++ b/python-api-examples/streaming_server.py @@ -584,7 +584,11 @@ class StreamingServer(object): path: str, request_headers: websockets.Headers, ) -> Optional[Tuple[http.HTTPStatus, websockets.Headers, bytes]]: - if "sec-websocket-key" not in request_headers: + if "sec-websocket-key" not in ( + request_headers.headers # For new request_headers + if hasattr(request_headers, "headers") + else request_headers # For old request_headers + ): # This is a normal HTTP request if path == "/": path = "/index.html"