如何优化响应对象Response的反馈机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计302个文字,预计阅读时间需要2分钟。
问题:图像函数的 return 和普通函数的 return 有什么区别。
图像函数会返回状态码(status)和 content-type(放置在http请求的headers中)。content-type 还会告知 http 请求的接收方如何解析返回。
问题:视图函数的 return 和 普通函数的 return 有什么区别。
视图函数会返回状态码(status)、content-type(放置在www.baidu.com'
}
content = '<html></html>'
response = make_response(content, 301)
response.headers = headers
return response
app.add_url_rule('/hello/', view_func=hello)
if __name__ == '__main__':
# 生产环境 nginx + uwsgi服务器
# 访问配置参数 因为app.config是dict的子类
app.run(debug=app.config['DEBUG'], host='0.0.0.0', port=8800)
方式二:逗号分隔返回
from flask import Flask, make_responseapp = Flask(__name__)
# 载入整个配置文件
app.config.from_object('config') # from_object需要接收模块的路径
def hello():
headers = {
'content-type': 'text/plain',
'location': 'www.baidu.com'
}
return '<html></html>', 301, headers
app.add_url_rule('/hello/', view_func=hello)
if __name__ == '__main__':
# 生产环境 nginx + uwsgi服务器
# 访问配置参数 因为app.config是dict的子类
app.run(debug=app.config['DEBUG'], host='0.0.0.0', port=8800)
本文共计302个文字,预计阅读时间需要2分钟。
问题:图像函数的 return 和普通函数的 return 有什么区别。
图像函数会返回状态码(status)和 content-type(放置在http请求的headers中)。content-type 还会告知 http 请求的接收方如何解析返回。
问题:视图函数的 return 和 普通函数的 return 有什么区别。
视图函数会返回状态码(status)、content-type(放置在www.baidu.com'
}
content = '<html></html>'
response = make_response(content, 301)
response.headers = headers
return response
app.add_url_rule('/hello/', view_func=hello)
if __name__ == '__main__':
# 生产环境 nginx + uwsgi服务器
# 访问配置参数 因为app.config是dict的子类
app.run(debug=app.config['DEBUG'], host='0.0.0.0', port=8800)
方式二:逗号分隔返回
from flask import Flask, make_responseapp = Flask(__name__)
# 载入整个配置文件
app.config.from_object('config') # from_object需要接收模块的路径
def hello():
headers = {
'content-type': 'text/plain',
'location': 'www.baidu.com'
}
return '<html></html>', 301, headers
app.add_url_rule('/hello/', view_func=hello)
if __name__ == '__main__':
# 生产环境 nginx + uwsgi服务器
# 访问配置参数 因为app.config是dict的子类
app.run(debug=app.config['DEBUG'], host='0.0.0.0', port=8800)

