1.get请求
①.通过地址栏?key=value
from flask import Flask,request
@app.route('/get1', methods=['GET']) def M1(): """①.地址栏?key=value取值""" name = request.args.get('name', '')#姓名 age = request.args.get('age', '')#年龄 return str(age) + "/" + name
效果:
②.通过地址栏
from flask import Flask,request @app.route('/get2/<int:age>/<name>',methods=['GET']) def M2(age,name): """②.地址栏取值""" return str(age) + "/" + name
效果:
2.post请求
①.Form表单
import os from flask import Flask,request from werkzeug.utils import secure_filename @app.route('/post1',methods=['POST']) def M3(): """①.Form表单取值""" name = request.form['name']#姓名 age = request.form['age']#年龄 #file对象 file = request.files['file'] #保存图片 file.save(os.path.join(os.getcwd(), secure_filename(file.filename))) return str(age) + "/" + name + "/" + file.filename
效果:
图片:
②.Json字符串
from flask import Flask,request @app.route('/post2',methods=['POST']) def M4(): """①.Json字符串取值""" user = eval(request.data) result = str(user['age'])#年龄 result += "/" + user['name']#姓名 #teach对象 teach = user['teach'] #循环对象 for t in teach: result+="/" + t['class'] #循环数组 for n in t['num']: result+= "/" + str(n) return result
效果: