Flask RESTful API
程序员文章站
2024-03-22 09:45:10
...
使用Flask RESTful API 进行接口开发
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
import json
webapp = Flask(__name__)
api = Api(webapp)
noOfVisitors = 0
##测试api接口
@api.resource('/hello', methods=['GET', 'POST'])
class HelloWorld(Resource):
def get(self):
return 'Hello - this is GET'
def post(self):
jsonData = request.get_json(cache=False)
print(json.dumps(jsonData, indent=2, sort_keys=True))
for key in jsonData:
print(key)
global noOfVisitors
noOfVisitors = noOfVisitors + 1
return jsonify(totalVisits=noOfVisitors)
if __name__ == '__main__':
webapp.run(host='0.0.0.0', port=8999, debug=True)
访问 http://localhost:8999/hello是,结果如下:
使用postman进行post调式,不会postman的请参考链接https://blog.csdn.net/fxbin123/article/details/80428216
结果如下:
查看端口: netstat -anp|grep 8999
关闭端口: kill -9 端口号