2016-01-26 37 views
0

从Python 3.4和urllib3.request调用Apple Search API时,我应该关闭证书验证并忽略警告吗?用Python 3.4和urllib3.request调用Apple Lookup API证书错误

我的尝试:

  1. 我第一次发现一个问题,当我提出用urllib.request里从一个虚拟环境(Python的工具为Visual Studio 2013),例如呼叫

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

    引发此错误:

    urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)

  2. 奇怪的是,当我从我的正常的Python 3.4环境相同的呼叫(即未在虚拟环境中),我收到没有错误。

  3. 但是,我想使用虚拟env,所以我想我会尝试使用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] certificate verify failed (_ssl.c:600)

    这是否仅仅意味着有苹果的服务器上有点毛病证书?

  4. 我改变了呼叫不需要认证:

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

    这勿庸置疑了警告:

    InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html

  5. 我禁用警告(显然不正常建议):

    urllib3.disable_warnings() 
    

回答

0

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

我只是想你做最新urllib3(主)和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中的错误,请在相应的项目上打开一个问题。 :)

+0

我可以在不同的Python虚拟环境中尝试此操作。我使用的版本是:urllib3是1.14,certifi是2015.11.20.1 – stifin