python flash 启动http 服务

  • pip安装flask
    pip install flask
    
  • get 和 post 示例
    import json
    from flask import Flask, request
    from wsgiref.simple_server import make_server
    
    py_service = Flask(__name__)  # flask 应用名称
    
    @py_service.route('/hello', methods=['GET'])
    def get_person_info():
        return 'Hello World'
    
    @py_service.route("/change", methods=['POST'])
    def get_separate():
        parameters = json.loads(request.data.decode())
        sentence = parameters['sentence']
        print(sentence)
        return sentence + ' -- ' + 'is changed'
    
    if __name__ == '__main__':
         server = make_server('127.0.0.1', 9000, py_service)
         server.serve_forever()
    
  • wsgiref是Python标准库的一个模块,是对WSGI的一个实现。其中wsgiref.simple_server是一个简单的WSGI HTTP Server
  • 测试结果
    • get
      image
    • post
      image
posted @ 2022-02-24 16:18  cc-31415926  阅读(193)  评论(0编辑  收藏  举报