from flask import Flask, request, jsonify
import json
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
#HTTP POST with application/x-www-form-urlencoded
@app.route('/login', methods=['POST'])
def login():
user = request.form.get('username')
pwd = request.form.get('passwd')
if user == 'abc' and pwd == '123':
return jsonify({'status':'ok'})
else:
return jsonify({'status':'bad'})
#HTTP POST with application/json
@app.route('/login2', methods=['POST'])
def login2():
data = request.get_data()
json_data = json.loads(data.decode("utf-8"))
if 'username' in json_data :
user = json_data['username']
if 'passwd' in json_data :
pwd = json_data['passwd']
if user == 'abc' and pwd == '123':
return jsonify({'status':'ok'})
else:
return jsonify({'status':'bad'})
#HTTP GET
@app.route('/info', methods=['GET'])
def info():
id = request.args["id"]
if id == 'abc' :
return jsonify({'status':'ok'})
else:
return jsonify({'status':'bad'})
if __name__ == '__main__':
app.run(host='0.0.0.0',
port= 3000)
使用curl命令进行测试
curl -v -d "username=abc&passwd=123" http://127.0.0.1:3000/login
curl -v -H "Content-Type:application/json" -X POST --data "{\"username\":\"abc\",\"passwd\":\"123\"}" http://127.0.0.1:3000/login2
curl -v "http://127.0.0.1:3000/info?id=abc"