我在以下远程服务器设置上使用 Python 3.6.5:
服务器:Windows 10
Python:3.6.5
请求:2.18.4
Pentaho:8.0
当我在服务器命令提示符中针对 URL 运行
request.get
时,它会按预期获取 JSON:
>>> import requests
>>> response = requests.get(url, headers=headers)
>>> json = response.json()
>>> print(json)
{'d': {'results': [{'_ ...
但是,当我在 Pentaho 8.0 的 CPython 中运行相同的脚本时,我得到了
RecursionError:超出最大递归深度
完整日志:
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\2\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)
如果安装了
gevent
,则需要对Python socket
进行猴子补丁才能配合(请参阅文档或这个github问题)。
因此
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)
更新:
我发现我的问题是因为我在应用程序的另一个地方引入了 locust,它使用 gevent==1.2.2。当我注释掉 gevent import 语句后,这个递归问题就消失了。值得检查一下您是否在应用程序中的某个位置引入了 gevent。
我最近遇到了同样的错误,但没有找到任何方法来解决这个问题。在我的情况下,我不得不使用 http 而不是 https,尽管这确实很危险。
from gevent import monkey as curious_george
# curious_george.patch_all(thread=False, select=False)
def stub(*args, **kwargs): # pylint: disable=unused-argument
pass
对于我的情况,我使用
reqto
,解决方案是在它周围放置一个 try: except: 块:
peers = None
try:
res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
peers = res.json()
if not res.ok or res.status_code >= 400:
print("WARNING: Cannot fetch peers:", domain)
update_last_error(domain, res)
except:
print("WARNING: Some error during get():", domain)
如果这对您不起作用,您可能需要清理您的
~/.local/lib/python3/site-packages/
不,不工作!也许只是巧合!
对于我的脚本,我必须在
import reqto
模块中 fba.commands
,而那里没有引用它。我创建了一个单独的分支,这样你就可以自己找到它:https://git.mxchange.org/?p=fba.git;a=shortlog;h=refs/heads/broken/missing-import-reqto
只需将所述导入行放在所述文件的标头中,其中存在其他 import
行,该代码就可以工作!
当我将
requests
库升级到版本 2.32.2 时,在 Flask 应用程序的上下文中出现了这个最大递归错误。该应用程序似乎运行良好,但我的自动化测试开始失败。 我采纳了上面提供的建议,并使用 conftest.py
import/patch_all() 行扩展了我的项目的 gevent
文件。然后运行时就不再出现问题了tox
。希望这对那里的人有帮助。