安装flask框架
pip3 install flask
开启服务代码
from flask import Flask, jsonify, json, request, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login/', methods=['post'])
def login():
print(request.form)
return 'login ok'
@app.route('/getUser/', methods=['get'])
def get_user():
print(request.args)
print(request.values)
return jsonify([{'id': 1}, {'name': 'xiaoming'}])
@app.route('/getUserById/', methods=['get'])
def get_user_byid(id):
print(id)
return jsonify([{'id': 1}, {'name': 'xiaoming'}])
@app.route('/addUser/', methods=['post'])
def add_user():
print(request.data)
return jsonify({'message': '操作成功'})
if __name__ == '__main__':
app.run(host='127.0.0.1',port=11012)
运行开启命令
python3 app.py