Python Flask,自定义响应信息,自定义状态码、响应头

demo.py(自定义响应信息):

# coding:utf-8

from flask import Flask, make_response


app = Flask(__name__)


@app.route("/index")
def index():
    # 1 使用元组,返回自定义的响应信息
    #            响应体    状态码  响应头
    # return "index page", 400, [("Name", "pyton"), ("City", "shenzhen")]  # 响应头可以是列表
    # return "index page", 400, {"Name": "python1", "City1": "sz1"}  # 响应头可以是字典
    # return "index page", "666 状态码说明信息", {"Name1": "python1", "City1": "sz1"}  # 可以是非标准状态码
    # return "index page", "666 状态码说明信息"   # 可以缺省响应头

    # 2 使用make_response 来构造响应信息
    resp = make_response("index page 2")  # 响应体
    resp.status = "666 状态码说明信息"   # 设置状态码 (可以是非标准的状态码)
    resp.headers["city"] = "sz"  # 设置响应头
    return resp


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

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/85332040