美文网首页
flask構建API:get和post

flask構建API:get和post

作者: 划船的浆不浪 | 来源:发表于2017-12-23 14:38 被阅读29次

方式一:get

# -*- coding: utf-8 -*-
"""
Created on Tue Jul 05 14:46:25 2016

@author: zedian
"""

from flask import Flask,request,jsonify
app = Flask(__name__)


@app.route('/classification/text=<usename>',methods=['GET','POST'])
def hello_world(usename):
    import jieba
    import jieba.analyse
    min_key=[line.strip() for line in open('min_keywords.txt').readlines()]  
    feimin_key=[line.strip() for line in open('feimin_keywords1.txt').readlines()]
    users = '%s' % usename
    tags = jieba.analyse.extract_tags(users, topK=10,allowPOS=('ns', 'n', 'vn'))
    k = 0;l = 0
    for tag in tags:
        if tag in min_key:
            k = k+1
        elif tag in feimin_key:
            l = l+1
    if k > l:
        return jsonify({'result': '1'})
    else:
        return jsonify({'result': '0'})
        
if __name__ == '__main__':
    app.run(host='0.0.0.0')

方式二:post

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 23 14:31:06 2016

@author:zedian
"""

from flask import Flask,jsonify

from flask import request
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
@app.route('/classification',methods=['POST'])

def hello_world():
    import jieba
    import jieba.analyse
    s = request.form['text']
    min_key=[line.strip() for line in open('testapipost/min_keywords.txt').readlines()]  
    feimin_key=[line.strip() for line in open('testapipost/feimin_keywords1.txt').readlines()]
    #users = '%s' % usename
    tags = jieba.analyse.extract_tags(s, topK=10,allowPOS=('ns', 'n', 'vn'))
    k = 0;l = 0
    for tag in tags:
        if tag in min_key:
            k = k+1
        elif tag in feimin_key:
            l = l+1
    if k > l:
        return jsonify({'minyi': '1'})
    else:
        return jsonify({'feimin': '0'})
        
if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000)

相关文章

网友评论

      本文标题:flask構建API:get和post

      本文链接:https://www.haomeiwen.com/subject/tbmuwttx.html