• Flask接口


    静态文件在static目录下可以访问

    # -*- coding:utf-8 -*-
    import json
    import random
    from flask import Flask, request
    from flask_cors import CORS
    
    # # 跨域支持
    app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/')
    CORS(app, supports_credentials=True, methods=["GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"], allow_headers='*')
    
    host = '0.0.0.0'
    post = '9999'
    
    # # 测试接口
    @app.route('/test', methods=['GET', 'POST'])
    def test():
        res_dict = {
            "ratio": random.random() / 10,
            "result_code": 200,
            "res_msg": 'ok'
        }
        return json.dumps(res_dict, ensure_ascii=False)
    
    
    # # 访问接口
    @app.route('/api/XXX', methods=['POST'])
    def get_XXX_main():
        if request.method == "POST":
            """ 读取入参的两种方式
            # # [1]
            # json_data   = json.loads(request.get_data().decode('utf-8'))
            # txt_message  = json_data.get('txt_message')
    
            # # [2]
            # txt_message  = request.form.get('txt_message')
            # print("txt_message=", txt_message)
            """
            txt_message  = request.form.get('txt_message')
            print("txt_message=", txt_message)
    
            # # 处理过程
            if len(txt_message) != 0:
                returnCase, returnData = xxxx(txt_message)
    
                # # 处理成功
                if returnCase == "0000":
                    res_dict = {
                        "inputQuestion": txt_message,
                        "returnCode": returnCase,
                        "returnData": returnData,
                        "returnMessage": "right! 666,666"
                    }
                else:
                    res_dict = {
                        "inputQuestion": txt_message,
                        "returnCode": returnCase,
                        "returnData": returnData,
                        "returnMessage": "some question! 555,555"
                    }
                    
            # # 错误
            else:
                res_dict = {
                    "inputQuestion": txt_message,
                    "returnCode": "1111",
                    "returnData": '',
                    "returnMessage": "The question input is empty! emmmmmm"
                }
    
            return json.dumps(res_dict, ensure_ascii=False)
    
    
    if __name__ == '__main__':
        app.run(host=host, port=post, threaded=True, debug=False)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    操作系统的奋斗(二)
    Python手写贝叶斯网络
    OOTD | 美式复古穿搭耳机,复古轻便的头戴式耳机推荐
    linux安装redis
    JS上传文件(base64字符串和二进制文件流)
    [网络工程师]-应用层协议-WWW与HTTP
    融合注意力机制和BSRU的工业互联网安全态势预测方法
    monorepo
    Angular中的Email正则表达式翻译
    [锁]:乐观锁与悲观锁
  • 原文地址:https://blog.csdn.net/zzZ_CMing/article/details/133884296