python 请求 google.com 抛出 SSLError

python requests to google.com throwing SSLError

在尝试使用我使用 Google 驱动器 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')],)",),))

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

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

我的 python 版本是 3.5.0,我正在研究 Windows 10。请注意,这不是一个重复的问题;在与此问题有关的其他问题中,问题出在网站的 SSL 证书上。如果 Google 的 SSL 证书存在我无法理解的问题。

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

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

我再次查看了@Juilen Cochennec 在他的评论中链接的 ,并注意到它建议直接从 commendline 使用 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 的安全浏览功能。它会扫描网站并在认为它们不安全时将其阻止。

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

结果是此过滤导致所有 google 域出现 SSL 错误,从而导致请求中断。要修复我所要做的就是 disable safe results for Google;甚至没有安全浏览本身,只有安全结果。这禁用了过滤,我的请求现在又可以工作了。

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

P.S。我发现 this 有助于查看 SSL 证书链 应该 是什么,这有助于我确定它是错误的。