| 1 |
Troubleshooting |
| 2 |
=============== |
| 3 |
|
| 4 |
The first step of troubleshooting issues in programs using PycURL is |
| 5 |
identifying which piece of software is responsible for the misbehavior. |
| 6 |
PycURL is a thin wrapper around libcurl; libcurl performs most of the |
| 7 |
network operations and transfer-related issues are generally the domain |
| 8 |
of libcurl. |
| 9 |
|
| 10 |
``setopt``-Related Issues |
| 11 |
------------------------- |
| 12 |
|
| 13 |
:ref:`setopt <setopt>` is one method that is used for setting most |
| 14 |
of the libcurl options, as such calls to it can fail in a wide variety |
| 15 |
of ways. |
| 16 |
|
| 17 |
``TypeError: invalid arguments to setopt`` |
| 18 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 |
|
| 20 |
This usually means the *type* of argument passed to ``setopt`` does not |
| 21 |
match what the option expects. Recent versions of PycURL have improved |
| 22 |
error reporting when this happens and they also accept more data types |
| 23 |
(for example tuples in addition to lists). If you are using an old version of |
| 24 |
PycURL, upgrading to the last version may help troubleshoot the situation. |
| 25 |
|
| 26 |
The next step is carefully reading libcurl documentation for the option |
| 27 |
in question and verifying that the type, structure and format of data |
| 28 |
you are passing matches what the option expects. |
| 29 |
|
| 30 |
``pycurl.error: (1, '')`` |
| 31 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 32 |
|
| 33 |
An exception like this means PycURL accepted the structure and values |
| 34 |
in the option parameter and sent them on to libcurl, and |
| 35 |
libcurl rejected the attempt to set the option. |
| 36 |
|
| 37 |
Until PycURL implements an error code to symbol mapping, |
| 38 |
you have to perform this mapping by hand. Error codes are |
| 39 |
found in the file `curl.h`_ in libcurl source; look for ``CURLE_OK``. |
| 40 |
For example, error code 1 means ``CURLE_UNSUPPORTED_PROTOCOL``. |
| 41 |
|
| 42 |
libcurl can reject a ``setopt`` call for a variety of reasons of its own, |
| 43 |
including but not limited to the requested functionality |
| 44 |
`not being compiled in`_ or being not supported with the SSL backend |
| 45 |
being used. |
| 46 |
|
| 47 |
Transfer-Related Issues |
| 48 |
----------------------- |
| 49 |
|
| 50 |
If your issue is transfer-related (timeout, connection failure, transfer |
| 51 |
failure, ``perform`` hangs, etc.) the first step in troubleshooting is |
| 52 |
setting the ``VERBOSE`` flag for the operation. libcurl will then output |
| 53 |
debugging information as the transfer executes:: |
| 54 |
|
| 55 |
>>> import pycurl |
| 56 |
>>> curl = pycurl.Curl() |
| 57 |
>>> curl.setopt(curl.VERBOSE, True) |
| 58 |
>>> curl.setopt(curl.URL, 'https://www.python.org') |
| 59 |
>>> curl.setopt(curl.WRITEDATA, open('/dev/null', 'w')) |
| 60 |
>>> curl.perform() |
| 61 |
* Hostname www.python.org was found in DNS cache |
| 62 |
* Trying 151.101.208.223... |
| 63 |
* TCP_NODELAY set |
| 64 |
* Connected to www.python.org (151.101.208.223) port 443 (#1) |
| 65 |
* found 173 certificates in /etc/ssl/certs/ca-certificates.crt |
| 66 |
* found 696 certificates in /etc/ssl/certs |
| 67 |
* ALPN, offering http/1.1 |
| 68 |
* SSL re-using session ID |
| 69 |
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256 |
| 70 |
* server certificate verification OK |
| 71 |
* server certificate status verification SKIPPED |
| 72 |
* common name: www.python.org (matched) |
| 73 |
* server certificate expiration date OK |
| 74 |
* server certificate activation date OK |
| 75 |
* certificate public key: RSA |
| 76 |
* certificate version: #3 |
| 77 |
* subject: |
| 78 |
* start date: Sat, 17 Jun 2017 00:00:00 GMT |
| 79 |
* expire date: Thu, 27 Sep 2018 12:00:00 GMT |
| 80 |
* issuer: C=US,O=DigiCert Inc,OU=www.digicert.com,CN=DigiCert SHA2 Extended Validation Server CA |
| 81 |
* compression: NULL |
| 82 |
* ALPN, server accepted to use http/1.1 |
| 83 |
> GET / HTTP/1.1 |
| 84 |
Host: www.python.org |
| 85 |
User-Agent: PycURL/7.43.0.1 libcurl/7.52.1 GnuTLS/3.5.8 zlib/1.2.8 libidn2/0.16 libpsl/0.17.0 (+libidn2/0.16) libssh2/1.7.0 nghttp2/1.18.1 librtmp/2.3 |
| 86 |
Accept: */* |
| 87 |
|
| 88 |
< HTTP/1.1 200 OK |
| 89 |
< Server: nginx |
| 90 |
< Content-Type: text/html; charset=utf-8 |
| 91 |
< X-Frame-Options: SAMEORIGIN |
| 92 |
< x-xss-protection: 1; mode=block |
| 93 |
< X-Clacks-Overhead: GNU Terry Pratchett |
| 94 |
< Via: 1.1 varnish |
| 95 |
< Fastly-Debug-Digest: a63ab819df3b185a89db37a59e39f0dd85cf8ee71f54bbb42fae41670ae56fd2 |
| 96 |
< Content-Length: 48893 |
| 97 |
< Accept-Ranges: bytes |
| 98 |
< Date: Thu, 07 Dec 2017 07:28:32 GMT |
| 99 |
< Via: 1.1 varnish |
| 100 |
< Age: 2497 |
| 101 |
< Connection: keep-alive |
| 102 |
< X-Served-By: cache-iad2146-IAD, cache-ewr18146-EWR |
| 103 |
< X-Cache: HIT, HIT |
| 104 |
< X-Cache-Hits: 2, 2 |
| 105 |
< X-Timer: S1512631712.274059,VS0,VE0 |
| 106 |
< Vary: Cookie |
| 107 |
< Strict-Transport-Security: max-age=63072000; includeSubDomains |
| 108 |
< |
| 109 |
* Curl_http_done: called premature == 0 |
| 110 |
* Connection #1 to host www.python.org left intact |
| 111 |
>>> |
| 112 |
|
| 113 |
The verbose output in the above example includes: |
| 114 |
|
| 115 |
- DNS resolution |
| 116 |
- SSL connection |
| 117 |
- SSL certificate verification |
| 118 |
- Headers sent to the server |
| 119 |
- Headers received from the server |
| 120 |
|
| 121 |
If the verbose output indicates something you believe is incorrect, |
| 122 |
the next step is to perform an identical transfer using ``curl`` command-line |
| 123 |
utility and verify that the behavior is PycURL-specific, as in most cases |
| 124 |
it is not. This is also a good time to check the behavior of the latest |
| 125 |
version of libcurl. |
| 126 |
|
| 127 |
.. _curl.h: https://github.com/curl/curl/blob/master/include/curl/curl.h#L456 |
| 128 |
.. _not being compiled in: https://github.com/pycurl/pycurl/issues/477 |