【问题标题】:Feedparser.parse() 'SSL: CERTIFICATE_VERIFY_FAILED'Feedparser.parse() 'SSL: CERTIFICATE_VERIFY_FAILED'
【发布时间】:2015-04-01 16:44:33
【问题描述】:

我在 feedparser 解析 HTTPS RSS 提要时遇到了这个 SSL 问题,我真的不知道该怎么做,因为在 feedparser 方面我找不到关于这个错误的任何文档:

>>> import feedparser
>>> feed = feedparser.parse(rss)
>>> feed
{'feed': {}, 'bozo': 1, 'bozo_exception': URLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),), 'entries': []}
>>> feed["items"]
[]
>>> 

【问题讨论】:

  • 你有来自 SSL 握手的捕获吗?
  • 这是您需要的吗? i.imgur.com/1rYydb6.png
  • 好的,您的客户端似乎拒绝服务器的证书,出现未知的证书颁发机构错误 您有哪些版本的 python 和 feedparser?您是否创建了自签名证书?
  • Python 2.7.9 和 feedparser 5.1.3 我没有创建任何证书,这只是直接从安装 Python 和 Feedparser 开始的,我还应该注意到这以前在 Python 3 中有效,但我不能再在这个项目中使用 Python 3
  • 不确定这是否对您有帮助,但这里有一个链接,我认为问题在于它还谈到了可能解决 linux.debian.bugs.dist.narkive.com/Fa81q1tS/… 的问题,一般来说,ssl 客户端通常可以选择忽略证书验证

标签: python python-2.7 ssl rss feedparser


【解决方案1】:

确保已安装ca-certificates

在缺少它的 docker 容器中使用 feedparser 时遇到了这个问题,只需安装它即可解决我的问题。

【讨论】:

    【解决方案2】:

    这是由于 Python 开始应用 certificate verification by default for stdlib http clients.

    可以在this Redhat article 中找到有关更改理由的详细说明。还有关于如何控制和解决这种新情况的信息。

    之前的两个参考资料都解释了如何避免在单一连接中进行证书验证 (这不是 feedparser 用户的解决方案)

    import ssl
    
    # This restores the same behavior as before.
    context = ssl._create_unverified_context()
    urllib.urlopen("https://no-valid-cert", context=context)
    

    目前,feedparser 用户只能通过 monkeypatching 避免证书验证,强烈建议这样做,因为它会影响整个应用程序。

    在应用程序范围内更改行为的代码如下(取自 PEP-476 的代码):

    import ssl
    
    try:
        _create_unverified_https_context = ssl._create_unverified_context
    except AttributeError:
        # Legacy Python that doesn't verify HTTPS certificates by default
        pass
    else:
        # Handle target environment that doesn't support HTTPS verification
        ssl._create_default_https_context = _create_unverified_https_context
    

    There is an issue on the feedparser tracker about this:How to fix SSL: CERTIFICATE_VERIFY_FAILED?.

    【讨论】:

      【解决方案3】:

      感谢 cmidi 的回答,即使用 ssl._create_default_https_context = ssl._create_unverified_context '猴子补丁'

      import feedparser
      import ssl
      if hasattr(ssl, '_create_unverified_context'):
          ssl._create_default_https_context = ssl._create_unverified_context
      feed = feedparser.parse(rss) #<<WORKS!!
      

      【讨论】:

        猜你喜欢
        • 2020-12-07
        • 2015-05-05
        • 2021-02-22
        • 2018-08-17
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 2019-09-30
        相关资源
        最近更新 更多