| 1 |
setopt(option, value) -> None |
| 2 |
|
| 3 |
Set curl session option. Corresponds to `curl_easy_setopt`_ in libcurl. |
| 4 |
|
| 5 |
*option* specifies which option to set. PycURL defines constants |
| 6 |
corresponding to ``CURLOPT_*`` constants in libcurl, except that |
| 7 |
the ``CURLOPT_`` prefix is removed. For example, ``CURLOPT_URL`` is |
| 8 |
exposed in PycURL as ``pycurl.URL``, with some exceptions as detailed below. |
| 9 |
For convenience, ``CURLOPT_*`` |
| 10 |
constants are also exposed on the Curl objects themselves:: |
| 11 |
|
| 12 |
import pycurl |
| 13 |
c = pycurl.Curl() |
| 14 |
c.setopt(pycurl.URL, "http://www.python.org/") |
| 15 |
# Same as: |
| 16 |
c.setopt(c.URL, "http://www.python.org/") |
| 17 |
|
| 18 |
The following are exceptions to option constant naming convention: |
| 19 |
|
| 20 |
- ``CURLOPT_FILETIME`` is mapped as ``pycurl.OPT_FILETIME`` |
| 21 |
- ``CURLOPT_CERTINFO`` is mapped as ``pycurl.OPT_CERTINFO`` |
| 22 |
- ``CURLOPT_COOKIELIST`` is mapped as ``pycurl.COOKIELIST`` |
| 23 |
and, as of PycURL 7.43.0.2, also as ``pycurl.OPT_COOKIELIST`` |
| 24 |
- ``CURLOPT_RTSP_CLIENT_CSEQ`` is mapped as ``pycurl.OPT_RTSP_CLIENT_CSEQ`` |
| 25 |
- ``CURLOPT_RTSP_REQUEST`` is mapped as ``pycurl.OPT_RTSP_REQUEST`` |
| 26 |
- ``CURLOPT_RTSP_SERVER_CSEQ`` is mapped as ``pycurl.OPT_RTSP_SERVER_CSEQ`` |
| 27 |
- ``CURLOPT_RTSP_SESSION_ID`` is mapped as ``pycurl.OPT_RTSP_SESSION_ID`` |
| 28 |
- ``CURLOPT_RTSP_STREAM_URI`` is mapped as ``pycurl.OPT_RTSP_STREAM_URI`` |
| 29 |
- ``CURLOPT_RTSP_TRANSPORT`` is mapped as ``pycurl.OPT_RTSP_TRANSPORT`` |
| 30 |
|
| 31 |
*value* specifies the value to set the option to. Different options accept |
| 32 |
values of different types: |
| 33 |
|
| 34 |
- Options specified by `curl_easy_setopt`_ as accepting ``1`` or an |
| 35 |
integer value accept Python integers and booleans:: |
| 36 |
|
| 37 |
c.setopt(pycurl.FOLLOWLOCATION, True) |
| 38 |
c.setopt(pycurl.FOLLOWLOCATION, 1) |
| 39 |
|
| 40 |
- Options specified as accepting strings by ``curl_easy_setopt`` accept |
| 41 |
``bytes`` and ``str`` with ASCII code points only. |
| 42 |
For more information, please refer to :ref:`unicode`. Example:: |
| 43 |
|
| 44 |
c.setopt(pycurl.URL, "http://www.python.org/") |
| 45 |
c.setopt(pycurl.URL, b"http://www.python.org/") |
| 46 |
|
| 47 |
- ``HTTP200ALIASES``, ``HTTPHEADER``, ``POSTQUOTE``, ``PREQUOTE``, |
| 48 |
``PROXYHEADER`` and |
| 49 |
``QUOTE`` accept a list or tuple of strings. The same rules apply to these |
| 50 |
strings as do to string option values. Example:: |
| 51 |
|
| 52 |
c.setopt(pycurl.HTTPHEADER, ["Accept:"]) |
| 53 |
c.setopt(pycurl.HTTPHEADER, ("Accept:",)) |
| 54 |
|
| 55 |
- ``READDATA`` accepts a file object or any Python object which has |
| 56 |
a ``read`` method. ``READDATA`` is emulated in PycURL via ``READFUNCTION``. |
| 57 |
The file should generally be opened in binary mode. Example:: |
| 58 |
|
| 59 |
f = open('file.txt', 'rb') |
| 60 |
c.setopt(c.READDATA, f) |
| 61 |
|
| 62 |
- ``WRITEDATA`` and ``WRITEHEADER`` accept a file object or any Python |
| 63 |
object which has a ``write`` method. ``WRITEDATA`` is emulated in PycURL |
| 64 |
via ``WRITEFUNCTION``. |
| 65 |
The file should generally be opened in binary mode. Example:: |
| 66 |
|
| 67 |
f = open('/dev/null', 'wb') |
| 68 |
c.setopt(c.WRITEDATA, f) |
| 69 |
|
| 70 |
- ``*FUNCTION`` options accept a function. Supported callbacks are documented |
| 71 |
in :ref:`callbacks`. Example:: |
| 72 |
|
| 73 |
import io |
| 74 |
b = io.BytesIO() |
| 75 |
c.setopt(pycurl.WRITEFUNCTION, b.write) |
| 76 |
|
| 77 |
- ``SHARE`` option accepts a :ref:`curlshareobject`. |
| 78 |
|
| 79 |
- ``STDERR`` option is not currently supported. |
| 80 |
|
| 81 |
It is possible to set integer options - and only them - that PycURL does |
| 82 |
not know about by using the numeric value of the option constant directly. |
| 83 |
For example, ``pycurl.VERBOSE`` has the value 42, and may be set as follows:: |
| 84 |
|
| 85 |
c.setopt(42, 1) |
| 86 |
|
| 87 |
*setopt* can reset some options to their default value, performing the job of |
| 88 |
:py:meth:`pycurl.Curl.unsetopt`, if ``None`` is passed |
| 89 |
for the option value. The following two calls are equivalent:: |
| 90 |
|
| 91 |
c.setopt(c.URL, None) |
| 92 |
c.unsetopt(c.URL) |
| 93 |
|
| 94 |
Raises TypeError when the option value is not of a type accepted by the |
| 95 |
respective option, and pycurl.error exception when libcurl rejects the |
| 96 |
option or its value. |
| 97 |
|
| 98 |
.. _curl_easy_setopt: https://curl.haxx.se/libcurl/c/curl_easy_setopt.html |