使用 Python 和 Flask 设计 RESTful API
#!/usr/bin/env python# coding=utf-8# import module# Load existing modelfrom flask import Flask, jsonify, request, abort, make_responseapp =Flask(__name__)@app.route('/tagger/v1.0/model', methods=['
·
#!/usr/bin/env python
# coding=utf-8
# import module
# Load existing model
from flask import Flask, jsonify, request, abort, make_response
app = Flask(__name__)
@app.route('/tagger/v1.0/model', methods=['GET'])
def get_model():
return jsonify({'models': "default"})
@app.route('/tagger/v1.0/submit', methods=['POST'])
def create_task():
if not request.json or not 'content' in request.json:
abort(400)
task = {
'content': request.json['content']
}
return jsonify({'labeled': __runModel(task['content'].decode('utf-8'))})
def __runModel(input=""):
pass
return output
if __name__ == '__main__':
app.run(debug=True)
测试
1 curl
curl -i -H "Content-Type: application/json" -X POST -d '{"content": "中国在亚洲"}' http://localhost:5000/tagger/v1.0/submit
2 python3
import requests
url = "http://localhost/tagger/v1.0/submit"
data = "{\"content\":\"中国在亚洲\"}"
data = data.encode('utf-8')
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
}
response=requests.request("POST",url,data=data,headers=headers)
print(response.text)
更多推荐
已为社区贡献1条内容
所有评论(0)