zoukankan      html  css  js  c++  java
  • flask_restful 接收post参数,返回json

    code

    from flask import Flask,jsonify,render_template,make_response
    from flask_restful import Api, Resource,request
    from flask_restful import reqparse
    
    #本文件所在目录
    workdir=os.path.abspath(os.path.dirname(__file__))
    
    app = Flask(__name__,static_url_path="",root_path=workdir)
    api = Api(app)
    
    encry_two=encry_two()
    class encrype(Resource):
        def post(self):
            parser = reqparse.RequestParser()
    
            # 第一个参数: 传递的参数的名称
            # 第二个参数(location): 传递参数的方式
            # 第三个参数(type): 验证参数的函数(可以自定义验证函数)
            parser.add_argument('args', location='json', type=str)
    
            # args是一个字典
            args = parser.parse_args()
    
            # 获取验证后的数据
            content = args.get('args')
            a=encry_two.encrypt_data(content)
            dic={}
            dic["before"]=content
            dic["encryped"]=a.decode()
            return jsonify(dic)
    
    class decrype(Resource):
        def post(self):
            parser = reqparse.RequestParser()
    
            # 第一个参数: 传递的参数的名称
            # 第二个参数(location): 传递参数的方式
            # 第三个参数(type): 验证参数的函数(可以自定义验证函数)
            parser.add_argument('args', location='json', type=str)
    
            # args是一个字典
            args = parser.parse_args()
    
            # 获取验证后的数据
            content = args.get('args')
            print(content)
            a=encry_two.get_decrype_str(content.encode())
            dic={}
            dic["before"]=content
            dic["decryped"]=a.decode()
            return jsonify(dic)
    
    class index(Resource):
        def get(self):
            #templates文件夹下面
            return render_template('aaa.html')
    
    api.add_resource(encrype, '/encrype')
    '''
    {
        "args":"221212"
    }
    '''
    api.add_resource(decrype,'/decrype')
    '''
    {
        "args":"jNKuAMkJZxl6hiPkFtTkTR6prQeTdD9Y7I/LpQ8yd1nuuF2In6UegLsdjtW1zaaD0ZILpRkjxrY7t8hf/J0ctwDLbNrvVN92/tMqXIK4iAiJTF45eUeSO5/e8+LA2+/1GMH/jTn9gqPZN+VnqW1CKaCmNHbKi0HsSWrEvv87p8Y="
    }
    '''
    api.add_resource(index,'/index')
    
    @api.representation("text/html")
    def out_html(data,code,headers):
        return make_response(data)
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0", port=5000)
        #app.run(debug=True)
  • 相关阅读:
    互质与欧拉函数学习笔记
    Luogu P4588 [TJOI2018]数学计算 题解
    Luogu P1072 Hankson 的趣味题 题解
    Luogu [POI2002][HAOI2007]反素数 题解
    Luogu P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题解
    JavaScript 实现简易版贪吃蛇(Day_13)
    IDEA 配置 Tomcat(详细)(Day_12)
    常用数据库连接池配置及使用(Day_11)
    大对象数据LOB的应用(Day_10)
    IDEA中配置maven 全解析教程(Day_08)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182105.html
Copyright © 2011-2022 走看看