0
点赞
收藏
分享

微信扫一扫

python 编写一个脚本 输入字符串 返回其长度 生成api接口

python 编写一个脚本 输入字符串 返回其长度 生成api接口


from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/get_length', methods=['POST'])
def get_length():
    data = request.get_json()
    if 'input_string' in data:
        input_string = data['input_string']
        length = len(input_string)
        result = {'length': length}
        return jsonify(result)
    else:
        return jsonify({'error': 'Input string not provided'}), 400

if __name__ == '__main__':
    app.run(debug=True)

这是一个使用Flask库的基本示例。你可以通过发送POST请求到/get_length端点,并在请求体中包含JSON数据,例如:

{
  "input_string": "Hello, World!"
}

它将返回:

{
  "length": 13
}

确保你已经安装了Flask库,可以使用以下命令:

pip install Flask

将上述代码保存到一个.py文件中,运行它,然后通过发送POST请求来测试你的API。

举报

相关推荐

0 条评论