"RecursionError: maximum recursion depth exceeded" from ssl.py: `super(SSLContext, SSLContext).options.__set__(self, value)`

"RecursionError: maximum recursion depth exceeded" from ssl.py: `super(SSLContext, SSLContext).options.__set__(self, value)`

我在以下远程服务器设置上使用 Python 3.6.5:

Server: Windows 10

Python: 3.6.5

Requests: 2.18.4

Pentaho: 8.0

当我 运行 request.get 针对服务器命令提示符中的 URL 时,它会按预期获得 JSON:

>>> import requests
>>> response = requests.get(url, headers=headers)
>>> json = response.json()
>>> print(json)
{'d': {'results': [{'_ ... 

然而,当我 运行 在 CPython 中为 Pentaho 8.0 使用相同的脚本时,我得到

RecursionError: maximum recursion depth exceeded

完整日志:

2018/04/13 15:02:17 - Get SP Doc List.0 - ERROR (version 8.0.0.0-28, build 8.0.0.0-28 from 2017-11-05 07.27.50 by buildguy) : Unexpected error
    2018/04/13 15:02:17 - Get SP Doc List.0 - ERROR (version 8.0.0.0-28, build 8.0.0.0-28 from 2017-11-05 07.27.50 by buildguy) : org.pentaho.di.core.exception.KettleException: 
    2018/04/13 15:02:17 - Get SP Doc List.0 - Traceback (most recent call last):
      File "C:\Users\ADMINI~1\AppData\Local\Temp\pyServer.py", line 299, in execute_script
        exec (script, _global_env)
      File "<string>", line 16, in <module>
      File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 72, in get
        return request('get', url, params=params, **kwargs)
      File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 58, in request
        return session.request(method=method, url=url, **kwargs)
      File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 508, in request
        resp = self.send(prep, **send_kwargs)
      File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 618, in send
        r = adapter.send(request, **kwargs)
      File "C:\Program Files\Python36\lib\site-packages\requests\adapters.py", line 440, in send
        timeout=timeout
      File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
        chunked=chunked)
      File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
        self._validate_conn(conn)
      File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
        conn.connect()
      File "C:\Program Files\Python36\lib\site-packages\urllib3\connection.py", line 314, in connect
        cert_reqs=resolve_cert_reqs(self.cert_reqs),
      File "C:\Program Files\Python36\lib\site-packages\urllib3\util\ssl_.py", line 269, in create_urllib3_context
        context.options |= options
      File "C:\Program Files\Python36\lib\ssl.py", line 465, in options
        super(SSLContext, SSLContext).options.__set__(self, value)
      File "C:\Program Files\Python36\lib\ssl.py", line 465, in options
        super(SSLContext, SSLContext).options.__set__(self, value)
      File "C:\Program Files\Python36\lib\ssl.py", line 465, in options
        super(SSLContext, SSLContext).options.__set__(self, value)
      [Previous line repeated 322 more times]
    RecursionError: maximum recursion depth exceeded

脚本:

import requests
import json


# By Filename
url = "https://myco.sharepoint.com/teams/dg/l/_api/web/lists/GetByTitle('eRetail%20Data%20Sources')/items?..."

authtoken = "Bearer eyJ..."

headers = {
    "Content-Type": "application/json;odata=verbose",
    "Accept": "application/json;odata=verbose",
    "Authorization": authtoken
}

response = requests.get(url, headers=headers)

json = response.json()
print('===========================')
print(json)

更新:

我发现我的问题是因为我在我的应用程序的另一个地方引入了locust,它使用gevent==1.2.2。在我注释掉 gevent import 语句之后,这个递归问题就没有了。值得检查一下您是否在应用程序的某处引入了 gevent。


我最近运行遇到了同样的错误,还没有找到解决这个问题的方法。在我的情况下,我不得不使用 http 而不是 https,尽管它确实 危险

如果安装了gevent,需要对pythonsocket进行monkey-patch配合(见documentation or this github issue)。

因此 gevent.monkey.patch_all() 丢失或未及早调用。

# at the beginning of the script
import gevent.monkey
gevent.monkey.patch_all()

# all the other stuff below, like for example
import requests

不要猴子修补 ssl:

from gevent import monkey
monkey.patch_all(ssl=False)
from gevent import monkey as curious_george
# curious_george.patch_all(thread=False, select=False)
def stub(*args, **kwargs):  # pylint: disable=unused-argument
    pass