flask项目,一个文件,运行起来就可以,,温习知识并记录,以便于后期使用
api.py
# -*-coding:utf-8 -*-
import json
from flask import Flask, request, jsonify, abort
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
# cbv
class TestView(Resource):
def get(self):
params = request.args
print(params)
result = {"status": 200, "data": [], "message": "success"}
return jsonify(result)
def post(self):
try:
print(json.loads(request.data))
print(request.get_data())
print(request.get_json())
except Exception as e:
print(e)
print(e.args)
# abort(404)
return jsonify({"status": 500})
def put(self):
pass
def delete(self):
pass
# fbv
@app.route('/flask/api/list', methods=['GET'])
def get_list():
print(request.args)
return jsonify({'articles': [1, 2, 3, 4, 5]})
api.add_resource(TestView, "/flask/test/")
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8879, debug=True)