未处理的异常:接收数据时连接已关闭

时间:2020-09-06 15:18:31

标签: python rest flutter

我有以下用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"

0 个答案:

没有答案