一.返回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'])
浏览器浏览效果
响应对象
查看返回头部
响应对象

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

相关文章:

  • 2021-12-16
  • 2021-06-23
  • 2021-05-22
  • 2022-01-29
  • 2022-12-23
  • 2021-05-30
  • 2022-01-31
  • 2021-10-08
猜你喜欢
  • 2021-12-18
  • 2021-05-15
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案