gpt4 book ai didi

python - 未处理的异常 : Connection closed while receiving data

转载 作者:行者123 更新时间:2023-12-05 07:01:42 28 4
gpt4 key购买 nike

我有以下用 flask (python) 编写的 rest API

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/load/<path>', methods=['GET', 'POST'])
def get_gene_list(path):
gene_list_dict = {}
print(path)
try:
with open(path, 'r') as f:
for line in f:
name, list = line.split('\t')
gene_list_dict[name] = list
except FileNotFoundError:
return jsonify({'sent': path, 'error': 'file not found'})

gene_list_dict['error'] = None
return jsonify(gene_list_dict)

if __name__ == '__main__':
app.run(debug=True)

dart 上的以下代码发送 GET 请求:

persistentFooterButtons: [
RaisedButton(
onPressed: () {
loadGeneList(
'SOME\\HARD\\CODED\\ABSOLUTE\\PATH');
},
child: Text('Load CSV'),
)

函数loadGeneList实现如下:

Future<void> loadGeneList(query) async {
String url = 'http://10.0.2.2:5000/load/$query';
http.Response response = await http.get(url, headers: {
"Content-Type": "application/json",
});
print(response.body);
}

当我触发请求时,我在 dart 控制台上得到以下回溯:

E/flutter (30778): [ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: Connection closed while receiving data
E/flutter (30778): #0 IOClient.send.<anonymous closure>
package:http/src/io_client.dart:50
E/flutter (30778): #1 _invokeErrorHandler (dart:async/async_error.dart:16:24)
E/flutter (30778): #2 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:282:9)
E/flutter (30778): #3 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (30778): #4 _HttpClientResponse.listen.<anonymous closure> (dart:_http/http_impl.dart:438:16)
E/flutter (30778): #5 _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter (30778): #6 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (30778): #7 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (30778): #8 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (30778): #9 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (30778): #10 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter (30778): #11 _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:132:11)
E/flutter (30778): #12 _addErrorWithReplacement (dart:async/stream_pipe.dart:180:8)
E/flutter (30778): #13 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:287:11)
E/flutter (30778): #14 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (30778): #15 _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter (30778): #16 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (30778): #17 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (30778): #18 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (30778): #19 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (30778): #20 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter (30778): #21 _SyncStreamControllerDispatch._sendError (dart:async/stream_controller.dart:812:19)
E/flutter (30778): #22 _StreamController._addError (dart:async/stream_controller.dart:690:7)
E/flutter (30778): #23 _StreamController.addError (dart:async/stream_controller.dart:642:5)
E/flutter (30778): #24 _HttpParser._reportBodyError (dart:_http/http_parser.dart:1155:22)
E/flutter (30778): #25 _HttpParser._onDone (dart:_http/http_parser.dart:864:9)

文件被正确解析,字典用 python 正确生成。

最佳答案

通过在 python 脚本中添加以下行解决了这个问题:

from werkzeug.serving import WSGIRequestHandler

app.run(...) 之前:

WSGIRequestHandler.protocol_version = "HTTP/1.1"

此答案发布为 edit问题Unhandled Exception: Connection closed while receiving data通过 OP Eliran Turgeman在 CC BY-SA 4.0 下。

关于python - 未处理的异常 : Connection closed while receiving data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63765727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com