Debian Codegraph @

doc/docstrings/curl_ws_close.rst

raw
1 ws_close(code=None, reason=None, encoding='utf-8') -> count
2
3 Send a WebSocket close frame. In detached mode this requires
4 ``CONNECT_ONLY=2``; inside an active ``WRITEFUNCTION`` callback it may
5 also be used to send a blocking reply.
6
7 Builds an RFC 6455 §5.5.1 close payload — an optional 2-byte big-endian
8 status *code* followed by an optional UTF-8 *reason* — and sends it as
9 a ``WS_CLOSE`` control frame. Prefer this over
10 ``ws_send(bytes, WS_CLOSE)``: the payload format is non-obvious.
11
12 *code* is the WebSocket close status code. Omitted (``None``) sends an
13 empty close payload. When specified, must be a valid wire code per RFC
14 6455 §7.4.1: ``1000`` (normal), ``1001`` (going away), ``1002``, ``1003``,
15 ``1007``-``1014``, or a private-use value in ``3000..4999``. Codes
16 ``1004``, ``1005``, ``1006``, ``1015`` are RFC-forbidden to send and
17 rejected.
18
19 *reason* may be a ``str`` or any bytes-like object. ``str`` is encoded
20 with *encoding* (UTF-8 by default). The resulting bytes must be valid
21 UTF-8 on the wire; invalid UTF-8 raises ``UnicodeDecodeError``,
22 non-encodable input raises ``UnicodeEncodeError``. ``reason`` without
23 ``code`` raises ``ValueError``. The combined payload (2-byte code +
24 reason) must not exceed 125 bytes (RFC 6455 §5.5).
25
26 Returns the number of bytes accepted by libcurl.
27
28 Same blocking / non-blocking semantics as :py:meth:`ws_send`. Calls
29 from other threads while ``perform()`` is running are rejected.
30
31 Corresponds to `curl_ws_send`_ with ``CURLWS_CLOSE``. Requires libcurl
32 7.86.0 or later. Raises ``pycurl.error`` for libcurl failures other
33 than ``CURLE_AGAIN``.
34
35 .. _curl_ws_send: https://curl.se/libcurl/c/curl_ws_send.html