首页 > 代码库 > python_接口开发

python_接口开发

一、GET和POST请求

from flask import Flask,jsonify

data = http://www.mamicode.com/{
‘name‘:‘Jerry‘,
‘sex‘:‘男‘,
‘age‘:‘18‘
}


app = Flask(__name__)

@app.route(‘/get_user‘,methods=[‘Get‘,‘post‘])

def get_user():
return jsonify(data)

app.run(host=‘0.0.0.0‘,port=8888,debug=True)

二、POST三种传参方式
1. k-v传参
2. url传参
3. json传参

from flask import Flask,jsonify,request
data = http://www.mamicode.com/{
‘Jerry‘:{‘sex‘:‘男‘,‘age‘:18},
‘Tom‘:{‘sex‘:‘男‘,‘age‘:20},
}

app = Flask(__name__)

@app.route(‘/get_user‘,methods=[‘post‘])

def get_user():
# username = request.form[‘username‘]#key-value传参
# username = request.args.get(‘username‘)#?+url传参
username = request.json.get(‘username‘)#json传参
if username in data:
return jsonify(data[username])
else:
return jsonify(500)

app.run(host=‘0.0.0.0‘,port=8888,debug=True)

python_接口开发