响应对象

一.返回html
新建一个config.py文件
Debug=True
fisher.py
from flask import Flask
 
 
app = Flask(__name__)
app.config.from_object("config")
 
 
@app.route("/hello")
def hello():
    return "<html><body><h1>h1biaoti</h1><p>hahhaha</p></body></html>"
 
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
浏览器显示效果

 
 
二.返回字符串
新建一个config.py文件
Debug=True
fisher.py
from flask import Flask,make_response
 
app = Flask(__name__)
app.config.from_object("config")
 
@app.route("/hello")
def hello():
 
    headers={
        "content-type":"text/plain"
    }
    response = make_response("<html><body><h1>h1biaoti</h1><p>hahhaha</p></body></html>")
    response.headers=headers
    return response
 
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
浏览器显示效果

 
 
 
三.返回状态码
新建一个config.py文件
Debug=True
fisher.py
from flask import Flask,make_response
 
app = Flask(__name__)
app.config.from_object("config")
 
@app.route("/hello")
def hello():
 
    headers={
        "content-type":"text/plain"
    }
    response = make_response("<html><body><h1>h1biaoti</h1><p>hahhaha</p></body></html>",404)
    response.headers=headers
    return response
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
浏览器显示效果

 
 
 
四.重定向
新建一个config.py文件
Debug=True
fisher.py
from flask import Flask,make_response
 
 
app = Flask(__name__)
app.config.from_object("config")
 
 
@app.route("/hello")
def hello():
 
 
    headers={
        "content-type":"text/plain",
        "location":"http://www.baidu.com"
    }
    
    response = make_response("<html><body><h1>h1biaoti</h1><p>hahhaha</p></body></html>",301)
    response.headers=headers
    return response
 
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
浏览器浏览

 
 
 
五.第二种写法,简化写法
from flask import Flask
 
 
app = Flask(__name__)
app.config.from_object("config")
 
 
@app.route("/hello")
def hello():
 
 
    headers={
        "content-type":"text/plain",
        "location":"http://www.baidu.com"
    }
 
 
    return "<html><body><h1>h1biaoti</h1><p>hahhaha</p></body></html>",301,headers
 
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
 

 
 
 
六.返回json
from flask import Flask
 
 
app = Flask(__name__)
app.config.from_object("config")
 
 
@app.route("/hello")
def hello():
 
 
    headers={
        "content-type":"application/json",
    }
 
 
    return '{"data":"hello world!"}',200,headers
 
 
app.run(host="0.0.0.0",debug=app.config['DEBUG'])
浏览器浏览效果
查看返回头部

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

posted @ 2020-12-24 10:19  anobscureretreat  阅读(69)  评论(0编辑  收藏  举报