故障排除¶
在使用pycurl的程序中解决问题的第一步是确定哪一个软件是导致错误行为的原因。pycurl是libcurl的一个薄包装;libcurl执行大多数网络操作,与传输相关的问题通常是libcurl的领域。
setopt
相关问题¶
setopt 是一种用于设置大多数libcurl选项的方法,因为对它的调用可能以多种方式失败。
TypeError: invalid arguments to setopt
¶
这通常意味着 type 传递给的参数 setopt
与选项预期的不匹配。pycurl的最新版本改进了出现这种情况时的错误报告,它们还接受更多的数据类型(例如除了列表之外的元组)。如果您使用的是旧版本的pycurl,升级到最新版本可能有助于解决此问题。
下一步是仔细阅读所讨论的选项的libcurl文档,并验证正在传递的数据的类型、结构和格式是否符合选项的预期。
pycurl.error: (1, '')
¶
这样的异常意味着pycurl接受了option参数中的结构和值,并将它们发送到libcurl,libcurl拒绝了设置选项的尝试。
在pycurl实现错误代码到符号的映射之前,必须手动执行此映射。在文件中找到错误代码 curl.h 在libcurl源中;寻找 CURLE_OK
. 例如,错误代码1表示 CURLE_UNSUPPORTED_PROTOCOL
.
libcurl可以拒绝 setopt
出于各种原因调用,包括但不限于请求的功能 not being compiled in 或者不支持正在使用的SSL后端。
转移相关问题¶
如果您的问题与传输相关(超时、连接故障、传输故障, perform
挂起等)故障排除的第一步是设置 VERBOSE
操作的标志。然后,libcurl将在传输执行时输出调试信息::
>>> import pycurl
>>> curl = pycurl.Curl()
>>> curl.setopt(curl.VERBOSE, True)
>>> curl.setopt(curl.URL, 'https://www.python.org')
>>> curl.setopt(curl.WRITEDATA, open('/dev/null', 'w'))
>>> curl.perform()
* Hostname www.python.org was found in DNS cache
* Trying 151.101.208.223...
* TCP_NODELAY set
* Connected to www.python.org (151.101.208.223) port 443 (#1)
* found 173 certificates in /etc/ssl/certs/ca-certificates.crt
* found 696 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL re-using session ID
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
* server certificate verification OK
* server certificate status verification SKIPPED
* common name: www.python.org (matched)
* server certificate expiration date OK
* server certificate activation date OK
* certificate public key: RSA
* certificate version: #3
* subject:
* start date: Sat, 17 Jun 2017 00:00:00 GMT
* expire date: Thu, 27 Sep 2018 12:00:00 GMT
* issuer: C=US,O=DigiCert Inc,OU=www.digicert.com,CN=DigiCert SHA2 Extended Validation Server CA
* compression: NULL
* ALPN, server accepted to use http/1.1
> GET / HTTP/1.1
Host: www.python.org
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
Accept: */*
< HTTP/1.1 200 OK
< Server: nginx
< Content-Type: text/html; charset=utf-8
< X-Frame-Options: SAMEORIGIN
< x-xss-protection: 1; mode=block
< X-Clacks-Overhead: GNU Terry Pratchett
< Via: 1.1 varnish
< Fastly-Debug-Digest: a63ab819df3b185a89db37a59e39f0dd85cf8ee71f54bbb42fae41670ae56fd2
< Content-Length: 48893
< Accept-Ranges: bytes
< Date: Thu, 07 Dec 2017 07:28:32 GMT
< Via: 1.1 varnish
< Age: 2497
< Connection: keep-alive
< X-Served-By: cache-iad2146-IAD, cache-ewr18146-EWR
< X-Cache: HIT, HIT
< X-Cache-Hits: 2, 2
< X-Timer: S1512631712.274059,VS0,VE0
< Vary: Cookie
< Strict-Transport-Security: max-age=63072000; includeSubDomains
<
* Curl_http_done: called premature == 0
* Connection #1 to host www.python.org left intact
>>>
上面示例中的详细输出包括:
域名解析
SSL连接
SSL证书验证
发送到服务器的邮件头
从服务器接收的邮件头
如果详细输出指示您认为不正确的内容,下一步是使用 curl
命令行实用程序,并验证该行为是否特定于pycurl,在大多数情况下不是这样。这也是检查libcurl最新版本行为的好时机。