【问题标题】:python requests to google.com throwing SSLErrorpython请求google.com抛出SSLError
【发布时间】:2018-09-06 13:03:32
【问题描述】:

在尝试使用用于使用 Google Drive python API 的旧脚本时,身份验证开始出现 SSL 错误。经过一番调查,我将原因缩小到 python 请求模块或它使用的 pyOpenSSL。我可以用这段代码来显示问题:

import requests

requests.get('https://google.com/')

最终的错误是:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\contrib\pyopenssl.py", line 444, in wrap_socket
    cnx.do_handshake()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\OpenSSL\SSL.py", line 1907, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\OpenSSL\SSL.py", line 1639, in _raise_ssl_error
    _raise_current_error()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 849, in _validate_conn
    conn.connect()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 356, in connect
    ssl_context=context)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\ssl_.py", line 359, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\contrib\pyopenssl.py", line 450, in wrap_socket
    raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\adapters.py", line 445, in send
    timeout=timeout
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\Documents\Email2notes\requtest.py", line 3, in <module>
    requests.get('https://google.com/')
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\adapters.py", line 511, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

我想我可以假设谷歌的证书没问题,而且代码是单行的,所以我认为异常一定是由其中一个模块引起的。我已经看到其他此类建议更新它们的问题 (for example)。但是我已经在使用最新版本的请求和 urllib3 和 pyOpenSSL(使用 pip 更新它们),我无法进一步更新它们,那么我该如何解决这个问题?

我已经按照elsewhere 的建议安装了requests[security]。还建议降级到旧版本的certifi,但是pip找不到建议的版本。

我的 python 版本是 3.5.0,我正在使用 Windows 10。请注意,这不是一个重复的问题;在与此问题有关的其他问题中,问题出在网站的 SSL 证书上。如果 Google 的 SSL 证书存在超出我能力的问题。

此外,使用verify=False 确实可以避免此代码的 sn-p 错误。但是在我的实际代码中(使用本身使用请求的google-auth-oauthlib)我无处可应用 - 我只是调用例如google_auth_oauthlib.flow.run_local_server(),根据here

【问题讨论】:

  • 是不是和这个有关? stackoverflow.com/questions/50246084/…
  • 很有可能。 Python 确实将我的 OpenSSL 版本列为'OpenSSL 1.0.2d 9 Jul 2015'。但即使安装了 1.0.2p 和 1.1.0i 版本,并更新了几个环境变量,Python 也无法识别它们;它仍然提供 1.0.2d 版本。知道我该如何解决吗?我能找到任何关于 OS X 和 brew 的答案,对我来说没多大用处。
  • 我最终将 python 本身更新到了 3.6.6。现在我的 OpenSSL 安装报告为 'OpenSSL 1.0.2o 27 Mar 2018',这应该足够新。但是原来的 SSL 错误仍然以同样的方式发生,所以我认为我们可以排除 OpenSSL 的版本是原因。

标签: python ssl python-requests pyopenssl


【解决方案1】:

我想通了! TL;DR:我的防病毒软件正在执行一些 HTTPS/TLS 过滤。

我再次查看了@Juilen Cochennec 在他的评论中链接的the answer,并注意到它建议直接从命令行使用 openssl 来检查实际返回的证书链。

所以我跑了

openssl s_client -showcerts -connect googleapis.com:443

我确实没有得到我期望的证书链;第一个中间证书不是由 Google 颁发的,而是由 Bullguard 颁发的。原来是

i:/C=GB/ST=Hounslow/L=Heathrow/O=BullGuard Ltd./OU=DevelTeam/CN=BullGuard SSL Proxy CA 

Bullguard 是我的防病毒软件的名称,所以我怀疑它以某种方式阻止了我的请求。经过一番研究,我找到了the precise cause,即 Bullguard 的安全浏览功能。如果它认为网站不安全,它会扫描网站并阻止它们。

其中的一个子集是安全结果,它会在搜索引擎(例如 Google)的结果旁边显示复选标记(或 X)。它似乎使用 HTTPS/TLS 过滤来做到这一点;它会在您和您的搜索结果之间插入自己,以便它可以修改带有复选标记的页面。现在看来,这在 Firefox 中甚至都不安全。勾选don't display,因为它无法获得安全的浏览结果。

结果是,此过滤导致所有 google 域的 SSL 错误,从而导致请求中断。要解决所有问题,我只需为 Google 提供 disable safe results;甚至不是安全浏览本身,只是安全的结果。这禁用了过滤,我的请求现在又可以工作了。

我希望这可以帮助其他人解决这个问题!

附:我发现this 有助于了解假定的 SSL 证书链是什么,这有助于我确定它是错误的。

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 2015-04-24
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多