使用flask API构建的两个应用程序尝试接收具有巨大JSON响应的响应,但错误10054失败,'现有连接被远程主机强行关闭'
我可以缩小这个问题,即当响应很大时,它会失败
@api.route('/endpoint', methods=['POST'])
def endpoint():
result = {small / huge dict}
return jsonify({'result': result}), 200
来电方
result = requests.post(url, params=data['args'], json=data['payload'])
return result.json()['result']
错误日志:
File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 1331, in getresponse
response.begin()
File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 321, in begin
self.headers = self.msg = parse_headers(self.fp)
File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 206, in parse_headers
line = fp.readline(_MAXLINE + 1)
File "C:\Program Files (x86)\Python36-32\lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
更新
尝试将响应作为计划文本和失败而不是更少,但仍然偶尔出现问题。
def response(output):
return Response(response_generator(output), mimetype='text/plain')
def response_generator(result):
result_str = json.dumps(result)
for row in [result_str[i:i + 1024*1024] for i in range(0, len(result_str), 1024*1024)]:
yield row
答案 0 :(得分:-1)
作为错误日志,问题可能如下:
请求返回字节,需要解码或Json解码 失败。我建议您更改代码如下:
result = requests.post(url, params=data['args'], json=data['payload']).text
return result
如果你需要使用json,你应该使用:
result = json.loads(requests.post(url, params=data['args'], json=data['payload']).text)