实例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# by vellhe 2021/12/01
from flask import Flask, abort, request, jsonify
app = Flask(__name__)
# 测试数据暂时存放
tasks = ["hello world"]
@app.route('/add_task/', methods=['POST'])
def add_task():
if not request.json or 'id' not in request.json or 'info' not in request.json:
abort(400)
task = {
'id': request.json['id'],
'info': request.json['info']
}
tasks.append(task)
return jsonify({'result': 'success'})
@app.route('/starttest/', methods=['GET'])
def get_task():
if not request.args or 'id' not in request.args:
# 没有指定id则返回全部
return jsonify(tasks)
else:
task_id = request.args['id']
task = filter(lambda t: t['id'] == int(task_id), tasks)
return jsonify(task) if task else jsonify({'result': 'not found'})
if __name__ == "__main__":
# 将host设置为0.0.0.0,则外网用户也可以访问到这个服务
app.run(host="0.0.0.0", port=80, debug=True)
启动
nohup python3 /home/yujianing/restful.py > nohup.log 2>&1 &
参考:
pythonFlask框架学习 - 简书Flask是由python实现的一个web微框架,让我们可以使用Python语言快速实现一个网站或Web服务。而且有对应的python3及python2版本。首先这边选择的是...https://www.jianshu.com/p/6452596c4edb