| 1 |
.. _curlobject: |
| 2 |
|
| 3 |
Curl Object |
| 4 |
=========== |
| 5 |
|
| 6 |
.. autoclass:: pycurl.Curl |
| 7 |
|
| 8 |
Curl objects have the following methods: |
| 9 |
|
| 10 |
.. automethod:: pycurl.Curl.close |
| 11 |
|
| 12 |
.. _setopt: |
| 13 |
.. automethod:: pycurl.Curl.setopt |
| 14 |
|
| 15 |
.. _perform: |
| 16 |
.. automethod:: pycurl.Curl.perform |
| 17 |
|
| 18 |
.. _perform_rb: |
| 19 |
.. automethod:: pycurl.Curl.perform_rb |
| 20 |
|
| 21 |
.. _perform_rs: |
| 22 |
.. automethod:: pycurl.Curl.perform_rs |
| 23 |
|
| 24 |
.. _getinfo: |
| 25 |
.. automethod:: pycurl.Curl.getinfo |
| 26 |
|
| 27 |
.. _getinfo_raw: |
| 28 |
.. automethod:: pycurl.Curl.getinfo_raw |
| 29 |
|
| 30 |
.. automethod:: pycurl.Curl.reset |
| 31 |
|
| 32 |
.. _unsetopt: |
| 33 |
.. automethod:: pycurl.Curl.unsetopt |
| 34 |
|
| 35 |
.. automethod:: pycurl.Curl.pause |
| 36 |
|
| 37 |
.. _recv: |
| 38 |
.. automethod:: pycurl.Curl.recv |
| 39 |
|
| 40 |
.. _recv_into: |
| 41 |
.. automethod:: pycurl.Curl.recv_into |
| 42 |
|
| 43 |
.. _send: |
| 44 |
.. automethod:: pycurl.Curl.send |
| 45 |
|
| 46 |
.. _errstr: |
| 47 |
.. automethod:: pycurl.Curl.errstr |
| 48 |
|
| 49 |
.. _errstr_raw: |
| 50 |
.. automethod:: pycurl.Curl.errstr_raw |
| 51 |
|
| 52 |
.. automethod:: pycurl.Curl.setopt_string |
| 53 |
|
| 54 |
WebSocket methods (libcurl 7.86.0 or later): |
| 55 |
|
| 56 |
.. _ws_send: |
| 57 |
.. automethod:: pycurl.Curl.ws_send |
| 58 |
|
| 59 |
.. _ws_recv: |
| 60 |
.. automethod:: pycurl.Curl.ws_recv |
| 61 |
|
| 62 |
.. _ws_recv_into: |
| 63 |
.. automethod:: pycurl.Curl.ws_recv_into |
| 64 |
|
| 65 |
.. _ws_meta: |
| 66 |
.. automethod:: pycurl.Curl.ws_meta |
| 67 |
|
| 68 |
.. _ws_close: |
| 69 |
.. automethod:: pycurl.Curl.ws_close |
| 70 |
|
| 71 |
PycURL supports libcurl's two documented WebSocket usage models: |
| 72 |
|
| 73 |
- **Detached mode.** Set ``CONNECT_ONLY`` to ``2``, call |
| 74 |
:py:meth:`perform` to drive the handshake, and then drive the |
| 75 |
connection yourself with :py:meth:`ws_send` and :py:meth:`ws_recv` |
| 76 |
(or :py:meth:`ws_recv_into`). In this mode, frame metadata is |
| 77 |
returned as the second element of each receive call's result. |
| 78 |
- **Callback-receive mode.** Set a ``WRITEFUNCTION`` callback, leave |
| 79 |
``CONNECT_ONLY`` unset (or ``0``), and call :py:meth:`perform`. |
| 80 |
libcurl drives the transfer and delivers each received frame |
| 81 |
chunk to the write callback. Call :py:meth:`ws_meta` from inside |
| 82 |
the callback to retrieve the current chunk's ``WsFrame`` metadata; |
| 83 |
``ws_meta()`` returns ``None`` outside that context. |
| 84 |
|
| 85 |
PycURL does not reassemble fragmented messages or manage the |
| 86 |
WebSocket close handshake. libcurl automatically replies to ping |
| 87 |
frames unless ``WS_NOAUTOPONG`` or ``WS_RAW_MODE`` is enabled. |
| 88 |
|
| 89 |
Frame metadata is exposed through the module-level ``WsFrame`` |
| 90 |
namedtuple (``age``, ``flags``, ``offset``, ``bytesleft``, ``len``). |
| 91 |
The ``flags`` field is a bitmask of the ``WS_*`` constants. |
| 92 |
|
| 93 |
**Caveats and sharp edges:** |
| 94 |
|
| 95 |
- **Do not combine ``CONNECT_ONLY=2`` with ``FORBID_REUSE``** — |
| 96 |
libcurl tears down the connection after the handshake |
| 97 |
``perform()`` returns and the first ``ws_send()`` fails with |
| 98 |
``CURLE_UNSUPPORTED_PROTOCOL``. |
| 99 |
- **A WebSocket handle is not thread-safe** — see |
| 100 |
:ref:`thread-safety`. |
| 101 |
- **``WS_RAW_MODE`` (via ``CURLOPT_WS_OPTIONS``) changes framing |
| 102 |
semantics.** libcurl ignores ``ws_send``'s ``flags`` argument and |
| 103 |
writes bytes verbatim. Raw-mode callers should pass bytes-like |
| 104 |
data; Python-side ``str``/bytes inference still runs but the |
| 105 |
flag bits never reach the wire. |
| 106 |
- **Replying inside a ``WRITEFUNCTION`` is allowed** — see |
| 107 |
:ref:`callbacks`. ``ws_send``/``ws_close`` behave as blocking |
| 108 |
sends in that context; ``ws_recv`` / ``ws_recv_into`` remain |
| 109 |
detached-only. |
| 110 |
- **Runtime probe**: ``'ws' in pycurl.version_info()[8]``. |
| 111 |
Compile-time: ``hasattr(pycurl, 'WS_TEXT')``. |