From 9cd3c45d6814892ad24cbf40220e2e6065d3ace9 Mon Sep 17 00:00:00 2001 From: dolfies Date: Wed, 4 Dec 2024 04:20:39 -0500 Subject: [PATCH] Readd Curl.ws_close() --- curl_cffi/curl.py | 18 ++++++++++++++++++ docs/api.rst | 1 + 2 files changed, 19 insertions(+) diff --git a/curl_cffi/curl.py b/curl_cffi/curl.py index 4e8ddcc..29c9b80 100644 --- a/curl_cffi/curl.py +++ b/curl_cffi/curl.py @@ -1,6 +1,7 @@ from __future__ import annotations import re +import struct import warnings from http.cookies import SimpleCookie from pathlib import Path @@ -433,6 +434,23 @@ def ws_send(self, payload: bytes, flags: CurlWsFlag = CurlWsFlag.BINARY) -> int: self._check_error(ret, "WS_SEND") return n_sent[0] + def ws_close(self, code: int = 1000, message: bytes = b"") -> int: + """Close a websocket connection. Shorthand for :meth:`ws_send` + with close code and message. Note that to completely close the connection, + you must close the curl handle after this call with :meth:`close`. + + Args: + code: close code. + message: close message. + + Returns: + 0 if no error. + + Raises: + CurlError: if failed. + """ + return self.ws_send(struct.pack("!H", code) + message) + class CurlMime: """Wrapper for the ``curl_mime_`` API.""" diff --git a/docs/api.rst b/docs/api.rst index 02db78d..a6137b4 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -24,6 +24,7 @@ Curl .. automethod:: close .. automethod:: ws_recv .. automethod:: ws_send + .. automethod:: ws_close AsyncCurl ~~~~~~