1.Flask-RESTful
Flask-RESTful是于快速构建REST API的Flask扩展
基本使:
1.安装扩展包flask-restful
2.导使,Api来在Flask中创建接,以类视图的形式实现
3.定义视图类,必须继承Resource
4.定义请求法
5.添加由
蓝图使:
from flask import Flask,Blueprint
from flask_restful import Api,Resource
# 蓝图和restful的使
# 蓝图的使步骤:1.创建蓝图对象;2.定义蓝图由;3.注册蓝图对象
app = Flask(__name__)
index_bp = Blueprint('index_hello',__name__)
# 把蓝图对象添加的api接中
api = Api(index_bp)
class IndexResource(Resource):
def get(self):
return {'get': 'hello world'}
def post(self):
return {'post': 'hello world'}
# 添加蓝图由
api.add_resource(IndexResource,'/')
# 注册蓝图对象
app.register_blueprint(index_bp)
# <Rule '/' (HEAD, POST, OPTIONS, GET) -> index_bp.indexresource>
# index_bp.indexresource;表示的是蓝图名称.视图类名称
if __name__ == '__main__':
print(app.url_map)
app.run()