利用Flask搭建Restful接口

2018-02-09 12:44:08来源:oschina作者:ttstone人点击

分享

项目需要访问Restful接口获取数据,程序开发过程中,用Flask实现了一个简单的RestFul接口。


from flask import Flask app = Flask(__name__) @app.route('/test') def test(): return 'this is test' if __name__ == '__main__': app.run(host='127.0.0.1',port = 5000)


获取不同的urlKey:


from flask import Flask


app = Flask(__name__)


@app.route("/")# def show(urlKey): return urlKey if __name__ == '__main__': app.run(host='127.0.0.1',port = 5000)


增加参数的识别:


from flask import Flask,request,jsonify import simplejson


app = Flask(__name__)


@app.route("/", methods=['GET', 'POST']) def show(urlKey): returnJson = {} datajson = None if request.method == 'POST': body = request.data if body == '':datajson = None else:datajson = simplejson.loads(body) if request.method == 'GET': args = simplejson.dumps(request.args) if args == '{}':datajson = None else:datajson = simplejson.loads(args) returnJson = {"url":urlKey,"param":datajson} return jsonify(returnJson) if __name__ == '__main__': app.run(host='127.0.0.1',port = 5000)

微信扫一扫

第七城市微信公众平台