【问题标题】:How to find root-cause of: urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)?如何找到根本原因: urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:590)?
【发布时间】:2015-12-24 09:01:35
【问题描述】:

我正在尝试测试breezometer.com. 的 API 当我在网页 https://breezometer.com/api/ 中输入我的 API 密钥时,它会返回预期的 JSON 回复。

但是,输入完全相同的数据 - 在以下模仿 Web 请求的 Python 脚本中(来自 Python 2 和 3):

$ cat test2.py
#!/usr/bin/env python

"""
Location from Google Maps: https://www.google.co.il/maps/place/Haifa+Port,+Haifa/@32.8267212,34.9852862,15z/data=!4m2!3m1!1s0x151dbbcd3a79ff47:0x8d20ff1e4833b549?hl=en

request: https://api.breezometer.com/baqi/?lat=32.8267212&lon=34.9852862&key=API_KEY
"""

from bs4 import BeautifulSoup
import urllib2

latitude = 32.8267212
longitude = 34.9852862
api_key = "XXXXXfb123e242839edeb10539dXXXXX"

url = "https://api.breezometer.com/baqi/?lat={0}&lon={1}&key={2}".format(latitude, longitude, api_key)

print(BeautifulSoup(urllib2.urlopen(url)))

我明白了:

$ python test2.py
Traceback (most recent call last):
  File "test2.py", line 18, in <module>
    print(BeautifulSoup(urllib2.urlopen(url)))
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

我查看过类似的 SO 线程(例如,Python 'requests' [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590) 等来自 https://stackoverflow.com/search?q=_ssl.c%3A590+CERTIFICATE_VERIFY_FAILED

我已将 https://breezometer.com/api/https://breezometer.com/ 证书(所有可用格式)导入 Chrome - 遵循 https://stackoverflow.com/a/31627786/1656850 建议:仍然,我收到 CERTIFICATE_VERIFY_FAILED 错误。

对如何调试此问题有何建议?

【问题讨论】:

  • 如果问题出在 python 上,将证书导入 Chrome 是没有意义的,因为 python 不使用来自 Chrome 的证书。相反,您需要为 urlopen 使用 capath 参数。

标签: python ssl certificate ssl-certificate urllib2


【解决方案1】:

使用 Python 2.7 添加 verify=False 即

requests.get(url, headers=user_agent, verify=False)

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 2019-07-03
    • 2018-07-29
    • 2017-06-01
    • 2017-02-05
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多