Python Flask无法设置内容长度

时间:2018-06-20 20:16:48

标签: python web-services flask

我正在使用Python和Flask创建一个快速的RESTful服务,以外部化配置数据并在应用启动时调用配置。

最初,我的服务器端代码是:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, request
app = Flask(__name__)


# this endpint returns backend server  connection settings
@app.route('/config/server_config/<string:customer_name>',
           methods=['GET'])
def get_config_server_config(customer_name):
    if customer_name == 'grant':
        return (jsonify([{
            'address': 'aaa.bb.cc.dd',
            'comm_hub_address': '192.168.137.38',
            'comm_hub_udp_beacon': '192.168.42.129',
            'comm_hub_port': '50000',
            'mqtt_port': '1883',
            'rest_port': '8000',
            'chat_port': '8010',
            }]), 200)
    if customer_name == 'orange':
        return (jsonify([{
            'address': 'aaa.bb.cc.dd',
            'comm_hub_address': '192.168.137.38',
            'comm_hub_udp_beacon': '192.168.42.129',
            'comm_hub_port': '50000',
            'mqtt_port': '1883',
            'rest_port': '8000',
            'chat_port': '8010',
            }]), 200)
    if customer_name == 'blue':
        return (jsonify([{
            'address': 'aaa.bb.cc.dd',
            'comm_hub_address': '192.168.137.38',
            'comm_hub_udp_beacon': '192.168.42.129',
            'comm_hub_port': '50000',
            'mqtt_port': '1883',
            'rest_port': '8000',
            'chat_port': '8010',
            }]), 200)
    else:
        return (jsonify({'error': 'customer does not exist'}), 404)

#if __name__ == '__main__':
    #app.run(debug=True, host='0.0.0.0', port=5000)
if __name__ == '__main__':
    app.run()

已正确返回JSON:

[[{{address“:” 137.78.81.53“,” chat_port“:” 8010“,” comm_hub_address“:” 192.168.137.38“,” comm_hub_port“:” 50000“,” comm_hub_udp_beacon“:” 192.168.42.129 “,” mqtt_port“:” 1883“,” rest_port“:” 8000“}],200]

但是...

问题是我的客户端Android通过EOFException抱怨Content-Length为0

由于未明确设置内容长度,因此我尝试在Python端设置内容长度:

 #!/usr/bin/python
    # -*- coding: utf-8 -*-
    from flask import Flask, jsonify, request
    app = Flask(__name__)


    # this endpint returns backend server  connection settings
    @app.route('/config/server_config/<string:customer_name>',
               methods=['GET'])
    def get_config_server_config(customer_name):
        if customer_name == 'grant':
            # set response object & return with http code
            resp = Flask.make_response(jsonify([{
                    'address': 'aaa.bb.cc.dd',
                    'chat_port': '8010',
                    'comm_hub_address': '192.168.137.38',
                    'comm_hub_port': '50000',
                    'comm_hub_udp_beacon': '192.168.42.129',
                    'mqtt_port': '1883',
                    'rest_port': '8000',
                    }]), 200)
            resp.mimetype = 'application/json'
            #set content length
            resp.headers['Content-Length'] = len(resp.get_data())
            return resp
        if customer_name == 'harris':
            return (jsonify([{
                'address': 'aaa.bb.cc.dd',
                'comm_hub_address': '192.168.137.38',
                'comm_hub_udp_beacon': '192.168.42.129',
                'comm_hub_port': '50000',
                'mqtt_port': '1883',
                'rest_port': '8000',
                'chat_port': '8010',
                }]), 200)
        if customer_name == 'hastings':
            return (jsonify([{
                'address': 'aaa.bb.cc.dd',
                'comm_hub_address': '192.168.137.38',
                'comm_hub_udp_beacon': '192.168.42.129',
                'comm_hub_port': '50000',
                'mqtt_port': '1883',
                'rest_port': '8000',
                'chat_port': '8010',
                }]), 200)
        else:
            return (jsonify({'error': 'customer does not exist'}), 404)

    #if __name__ == '__main__':
        #app.run(debug=True, host='0.0.0.0', port=5000)
    if __name__ == '__main__':
        app.run()

但是在浏览器中我遇到了错误:

内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序错误。

,然后在日志和命令行窗口中即时获取

AttributeError:“响应”对象没有属性“ response_class” 127.0.0.1--[2018年6月20日13:08:44]“ GET / config / server_config / grant HTTP / 1.1” 500-

如何使用Flask软件包设置响应对象的内容长度?

谢谢

0 个答案:

没有答案