flask 的make_response来构造响应信息

#_*_ encoding: utf-8 _*_   @author: ty  hery   2019/12/20

from  flask import  Flask, request, abort, Response, make_response
# from werkzeug.routing import BaseConverter

app = Flask(__name__)

@app.route('/index',methods=['GET'])
def index():
    # 1,使用元祖返回自定义的响应信息
    #         响应体     状态码           响应头
    # return 'index page00', 401, [('Itcas','python00'),("City","shenzhen01")] # 默认是返回元祖的形式,无论用不用()
    # return ('index page00', 400, [('Itcas','python01'),("City","shenzhen00")])
    # return 'index page001', 666 , {'Itcas':'python',"City":"sz00"}
    # return 'index page', "666 itcast status", {'Itcas':'python',"City":"sz00"}
    return 'index page666', "666 itcast status"

    # 2,使用make_esponse来构造响应信息
    resp = make_response("index page 2")
    resp.status = "999 itcast"  # 设置状态码
    resp.headers['city'] = 'sz001'  # 设置响应头
    return resp

# 定义错误处理的方法
@app.errorhandler(404)
@app.errorhandler(403)
def handler_404_error(err):
    '''自定义处理404错误的方法,这个函数返回值是前端用户看到的最终结果'''
    return u"出现了404错误了,错误信息:%s" %err


if __name__ == '__main__':
    print('--哈哈01--',app.url_map,'--哈哈01--')
    app.run(debug=True)
写入自己的博客中才能记得长久
原文地址:https://www.cnblogs.com/heris/p/14651103.html