【问题标题】:Apple Lookup API certificate error when called with Python 3.4 and urllib3.request使用 Python 3.4 和 urllib3.request 调用 Apple Lookup API 证书错误
【发布时间】:2016-05-03 03:06:34
【问题描述】:

从 Python 3.4 和 urllib3.request 调用 Apple Search API 时是否应该关闭证书验证并忽略警告?

我尝试了什么:

  1. 当我使用 urllib.request 从虚拟环境(使用 Visual Studio 2013 的 Python 工具)进行调用时,我首先发现了一个问题,例如

    r = urllib.request.urlopen('https://itunes.apple.com/lookup?id=429313263')
    

    引发此错误:

    urllib.error.URLError:

  2. 奇怪的是,当我从正常的 Python 3.4 环境(即不在虚拟环境中)发出相同的调用时,我收到 no 错误。

  3. 但是,我想使用虚拟环境,所以我想我会尝试使用 urllib3.request。当我要求证书验证时,它也失败了:

    http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
    r = http.request('GET', 'https://itunes.apple.com/lookup?id=429313263')
    

    urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:600)

    这是否仅仅意味着苹果服务器上的证书有问题?

  4. 我将调用更改为不需要认证:

    http = urllib3.PoolManager(cert_reqs='CERT_NONE',assert_hostname=False)
    

    不出所料地给出了警告:

    InsecureRequestWarning:正在发出未经验证的 HTTPS 请求。强烈建议添加证书验证。见:https://urllib3.readthedocs.org/en/latest/security.html

  5. 我禁用了警告(显然通常不建议这样做):

    urllib3.disable_warnings()
    

【问题讨论】:

    标签: python-3.x urllib3


    【解决方案1】:

    您使用的是哪个版本的 urllib3 和证书?

    我刚刚尝试了你在最新的 urllib3 (master) 和 certifi (2015.11.20.1) 上所做的,似乎对我有用:

    (in a virtualenv) $ python
    Python 3.5.1 (default, Dec 27 2015, 02:23:23)
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import urllib3, certifi
    >>> http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
    >>> r = http.request('GET', 'https://itunes.apple.com/lookup?id=429313263')
    >>> r.status
    200
    

    也适用于 2.7.11。不幸的是,我现在没有可用的 Python 3.4 安装。如果您设法将此归结为 urllib3 或 certifi 中的错误,请在相应项目上打开一个问题。 :)

    【讨论】:

    • 我可以在不同的 Python 虚拟环境中尝试这个。我使用的版本是:urllib3 是 1.14,certifi 是 2015.11.20.1
    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 2013-08-06
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    相关资源
    最近更新 更多