python - flask common interface development (simple case)

python - flask common interface development (simple cases)
original big loafing Posted on 2019-01-24 11:34:06 read the number 5208 collection
expanded

version: python3.5 +

module: flask

goal: to develop a method accepts only get interface receiving parameters and Age name, and returns the corresponding content.

    Import the Flask the Flask from, Request
    Import json
     
    App = the Flask (__ name__)
     
    # method accepts only get access to
    @ app.route ( "/ test_1.0", Methods = [ "GET"])
    DEF the Check ():
        # Returns the default content
        return_dict = { 'return_code': '200 ', 'return_info': ' treatment success', 'Result': False}
        # determines whether the parameter is empty
        iF request.args None iS:
            return_dict [ 'the return_code'] = '5004'
            return_dict [ 'return_info'] = 'parameter request is empty'
            return JSON.
        # Get the incoming parameters params
        get_Data request.args.to_dict = ()
        name = get_data.get ( 'name')
        Age = get_data.get ( 'Age')
        # operation parameters
        return_dict [ 'result'] = tt ( name, Age)
     
        return json.dumps (return_dict, ensure_ascii = False)
     
    # function function
    DEF tt (name, Age):
        result_str = "% s% s years old this year,"% (name, Age)
        return result_str
     
    IF __name__ == "__main__ ":
        app.run (Debug = True)

after pycharm the machine operates as follows:

using a transmission request to the postman tool interface address 127.0.0.1:5000/test_1.0

results are as follows:

 

 

2. changed only if the interface access method supports post code as follows:

    from the Flask Import the Flask,Request
    Import json
     
    App = the Flask (__ name__)
     
    # access method only accepts POST
    app.route @ ( "/ test_1.0", Methods = [ "the POST"])
    DEF Check ():
        # default return content
        return_dict = { 'return_code': ' 200', 'return_info': ' treatment success',' Result ': False}
        # determines whether the incoming data is empty json
        iF request.get_data () None iS:
            return_dict [' the return_code '] =' 5004 '
            return_dict [' return_info '] =' parameter request is empty '
            return json. dumps (return_dict, ensure_ascii = False)
        # acquired parameter passed
        the get_Data request.get_data = ()
        # of bytes passed parameter types, need to be converted into JSON
        the get_Data = json.loads (the get_Data)
        name = get_Data.get ( 'name ')
        Age = get_Data.get (' Age ')
        # Operation parameters
        return_dict [ 'result'] = tt (name, age)
     
        json.dumps return (return_dict, ensure_ascii = False)
     
    # function function
    DEF tt (name, Age):
        result_str = "% s% s years old this year,"% (name, Age)
        return result_str
     
    IF __name__ == "__main__":
        App. run (debug = True)

using postman test interface:

 

simple interface development generally is the case.
----------------
Disclaimer: This article is CSDN blogger "big lazy" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/t8116189520/article/details/86623320

Guess you like

Origin www.cnblogs.com/kofsony/p/12085284.html