【问题标题】:pycurl fails but curl (from bash) works in ubuntupycurl 失败,但 curl(来自 bash)在 ubuntu 中工作
【发布时间】:2010-10-08 18:27:48
【问题描述】:

我正在使用 curl 和 pycurl 连接到安全的 3rd 方 api,当我使用 pycurl 时,我从服务器返回身份验证错误,但是当我在命令行上使用 curl 并执行相同的操作时,它可以工作.我将两者都设置为详细模式,并看到请求中有一些差异,但我似乎无法弄清楚错误是什么。

他们似乎使用了不同的加密方法,也许这就是问题所在?如果有人对尝试使用 pycurl 的不同选项有想法或建议重新编译 pycurl 以像 curl 一样工作,那将是很棒的。谢谢。

这是我的 pycurl 设置,仅供参考:

    buffer = cStringIO.StringIO()

    curl = pycurl.Curl()
    curl.setopt(pycurl.VERBOSE,1) 
    curl.setopt(pycurl.POST, 1)
    curl.setopt(pycurl.POSTFIELDS, post_data)
    curl.setopt(pycurl.TIMEOUT_MS, self.HTTP_TIMEOUT)
    curl.setopt(pycurl.URL, url)
    curl.setopt(pycurl.FOLLOWLOCATION, self.HTTP_FOLLOW_REDIRECTS)
    curl.setopt(pycurl.MAXREDIRS, self.HTTP_MAX_REDIRECTS)
    curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
    curl.setopt(pycurl.NOSIGNAL, 1)
    curl.setopt(pycurl.SSLCERT, self.path_to_ssl_cert)

    curl.setopt(pycurl.SSL_VERIFYPEER, 0)

    # 1/0

    try:
        curl.perform()

...

哦,最后一件事:我正在使用的同一 python 脚本在我的 Mac 笔记本电脑上运行,但在我尝试设置的 ubuntu 服务器上却无法运行。

python test.py 
18:09:13,299 root INFO fetching: https://secure.....
* About to connect() to secure.... 1129 (#0)
*   Trying 216....... * connected
* Connected to secure.... port 1129 (#0)
* found 102 certificates in /etc/ssl/certs/ca-certificates.crt
*        server certificate verification OK
*        common name: secure.... (matched)
*        server certificate expiration date OK
*        server certificate activation date OK
*        certificate public key: RSA
*        certificate version: #3
*        subject: .......
*        start date: Sat, 14 Feb 2009 22:45:27 GMT
*        expire date: Mon, 15 Feb 2010 22:45:27 GMT
*        issuer: ...
*        compression: NULL
*        cipher: AES 128 CBC
*        MAC: SHA
User-Agent: PycURL/7.16.4
Host: secure....
Accept: */*
Content-Length: 387
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 200 OK
< Content-Length: 291
< 
* Connection #0 to host secure.... left intact
* Closing connection #0


 curl -v -d '...' --cert cert.pem  https://secure....
* About to connect() to secure.... port 1129 (#0)
*   Trying 216....... connected
* Connected to secure.... port 1129 (#0)
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
* SSLv2, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Request CERT (13):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS handshake, CERT verify (15):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using AES256-SHA
* Server certificate:
*        subject: .......
*        start date: 2009-02-14 22:45:27 GMT
*        expire date: 2010-02-15 22:45:27 GMT
*        common name: secure.... (matched)
*        issuer: ... Certificate Authority
* SSL certificate verify ok.
> User-Agent: curl/7.16.4 (i486-pc-linux-gnu) libcurl/7.16.4 OpenSSL/0.9.8e zlib/1.2.3.3 libidn/1.0
> Host: secure....:1129
> Accept: */*
> Content-Length: 387
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 200 OK
< Content-Length: 342

【问题讨论】:

    标签: python ssl curl https pycurl


    【解决方案1】:

    Ubuntu pycurl 使用 GnuTLS,而 ubuntu curl 命令行使用 OpenSSL。 存在差异,例如支持的证书格式。

    我无法理解 ubuntu 开发者/打包者做出的这个决定。我偶然发现了一次,无法解决它,幸运的是,除了 ubuntu 还有其他发行版 :-)

    你总是可以尝试向“人性对他人”抱怨。

    【讨论】:

      【解决方案2】:

      我在理解您发布的代码/输出片段时有些困难。是否包含实际的错误消息?

      SSL/TLS 的问题通常是由于 X.509 证书基础架构造成的。有像 Verisign、RapidSSL 等“证书颁发机构”(CA),它们对服务器的证书进行数字“签名”。要检查这些签名,您需要签署您要连接的服务器(“颁发者”)证书的 CA 的所谓“根证书”。

      通常操作系统会预装大量证书。通常浏览器、操作系统和某些库都有自己的证书列表。在 Mac 上,如果您启动程序“Keychain Access”并打开“System Roots”钥匙串,您可以看到它们。

      所以我建议您检查 Ubuntu 中是否缺少证书,如果是,则将其添加到那里。 (也许这些都保存在 /etc/ssl/certs/ 中)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-22
        • 2019-03-22
        • 2016-06-21
        • 1970-01-01
        相关资源
        最近更新 更多