乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      flask的post,get請求及獲取不同格式的參數(shù)

       印度阿三17 2019-10-29

      1 獲取不同格式參數(shù)

      1.0 獲取json參數(shù)

      • Demo

      from flask import Flask, request, jsonify
      app = Flask(__name__)
      
      @app.route('/jsonargs/<string:args_1>', methods=['POST'])
      def json_args(args_1):
          args_2 = request.json.get("args_2")
          args_3 = request.json['args_3']
          return jsonify({"args_1":args_1, "args_2":args_2, "args_3":args_3})
      
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8080, debug=True)
      • request

      1.2 獲取form參數(shù)

      • Demo

      from flask import Flask, request, jsonify
      
      app = Flask(__name__)
      @app.route('/formargs/<int:args_1>', methods=['POST'])
      def form_args(args_1):
          args_2 = request.form.get('args_2')
          args_3 = request.form['args_3']
          return jsonify({"args_1":args_1, "args_2":args_2, "args_3":args_3})
      
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8080, debug=True)
      • request

      1.3 get獲取地址欄參數(shù)

      from flask import Flask, request, jsonify
      app = Flask(__name__)
      
      @app.route('/getargs', methods=['GET', 'POST'])
      def get_args():
          args_1 = request.args.get("args_1")
          args_2 = request.args.get("args_2")
          args_3 = request.args.get("args_3")
          return jsonify({"args_1":args_1, "args_2":args_2, "args_3":args_3})
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8080, debug=True)

      Request

      1.4 獲取file文件

      from flask import Flask, request, jsonify
      import os
      basedir = os.path.abspath(os.path.dirname(__name__))
      app = Flask(__name__)
      
      @app.route('/imageprocess', methods=['GET', 'POST'])
      def image_preprocess():
          # get upload image and save
          image = request.files['image']
          path = basedir   "/source_images/"
          file_path = path   image.filename
          image.save(file_path)
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8080, debug=True)

      1.5 獲取任何格式

      from flask import Flask, request
      
      app = Flask(__name__)
      
      def upload_data():
          data = request.values.get("input")
          return jsonify({"data type":"successfully upload!"})
      if __name__ == "__main__":
          app.run(host="0.0.0.0", port=8090, debug=True)

      2 文件格式解析

      from flask import Flask, jsonify, request,abort
      import os
      
      app = Flask(__name__)
      def path_get():
          path = os.path.abspath(os.path.dirname(__name__))
          return path
      
      @app.route('/connect', methods=["GET"])
      def connect():
          return jsonify({"connect state":"successfully connect!"})
      
      @app.route('/upload_data', methods=["GET", "POST"])
      def upload_data():
          # 判斷form文件是否為空及是否是form文件
          if request.form and 'input' in request.form:
              upload_data = request.form['input']
              form_type = request.form
              print("form type: {}".format(form_type))
              # return jsonify({"input data":upload_data})
              return jsonify({"form data type":form_type})
          # 判斷json文件是否為空及是否是json文件
          elif request.json and 'input' in request.json:
              upload_data = request.json['input']
              # return jsonify({"input data":upload_data})
              # return jsonify({"input test":"success"})
              json_type = request.json
              print("json type: {}".format(json_type))
              return jsonify({"form data type":json_type})
          # 判斷files文件是否為空及是否是files文件
          elif 'input' in request.files:
              file = request.files["input"]
              path = path_get()   "/images/test.png"
              # file.save(path)
              files_type = request.files
              print("files type: {}".format(files_type))
              return jsonify({"path":path})
              
              # return jsonify({"form data type":files_type})
      
          else:
              abort(400)
      
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8098, debug=True)

      Result

      # form數(shù)據(jù)
      form type: ImmutableMultiDict([('input', 'dancer')])
      {
          "form data type": {
              "input": "dancer"
          }
      }
      =======================
      # json數(shù)據(jù)
      json type: {'input': 'jumper'}
      {
          "form data type": {
              "input": "jumper"
          }
      }
      =======================
      # files文件
      files type: ImmutableMultiDict([('input', <FileStorage: '1.jpeg' ('image/jpeg')>)])

      Analysis
      (1) form數(shù)據(jù)是可遍歷的字典,可使用字典屬性;
      (2) json數(shù)據(jù)為字典,可使用字典屬性;
      (3) files數(shù)據(jù)為可遍歷的字典,可使用字典屬性,通過key判斷是否存在該key,如input這個鍵;
      (4) 當使用數(shù)據(jù)中的一種格式時,其他的數(shù)據(jù)為None,不可遍歷,因此直接使用if判斷是否為該類型的數(shù)據(jù),會拋出錯誤TypeError: argument of type 'NoneType' is not iterable,所以通過先判斷數(shù)據(jù)是否為空,然后判斷是否為指定格式,解除錯誤;

      3 獲取checkbox數(shù)據(jù)

      3.1 獲取一個checkbox數(shù)據(jù)

      • html

      <p><input type="checkbox" name="checkbox_name" value="選擇框">選擇框</p>
      • flask

      data = request.values.get("checkbox_name")

      3.2 獲取多個checkbox數(shù)據(jù)

      • html

      <p><input type="checkbox" name="checkbox_names" value="選擇1">選擇1</p>
      <p><input type="checkbox" name="checkbox_names" value="選擇1">選擇2</p>
      <p><input type="checkbox" name="checkbox_names" value="選擇1">選擇3</p>
      • flask

      datas = request.values.getlist("checkbox_names")

      3.3 分級選擇

      參考:https://www.cnblogs.com/kaituorensheng/p/4529113.html

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      
      
      <script>
      function allSelect(check_v, checkname)
      {
          var v_item = document.getElementsByName(check_v);
          var items = document.getElementsByName(checkname);
          for (var i = 0; i < items.length;   i)
          {
              if (v_item[0].checked)
              {
                  items[i].checked = true;
              }
              else
              {
                  items[i].checked = false;
              }
          }
      }
      
      function singleSelect2parent(check_v, checkname)
      {
          var v_item = document.getElementsByName(check_v);
          var items = document.getElementsByName(checkname);
          var childStatus = true;
          for (var i = 0; i < items.length;   i)
          {
              childStatus = (childStatus && items[i].checked);
          }
          if (childStatus)
          {
              v_item[0].checked = true;
          }
          else
          {
              v_item[0].checked = false;
          }
      }
      
      </script>
      </head>
      <body>
      
      <p> <input type="checkbox" checked name="checkbox_v1" value="version1" onclick="allSelect('checkbox_v1', 'checkbox1')">默認全選</p>
      <ul>
          <p> <input type="checkbox" checked name="checkbox1" value="layer1" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">tiger_roads</p>
          <p> <input type="checkbox" checked name="checkbox1" value="layer2" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">poly_landmarks</p>
          <p> <input type="checkbox" checked name="checkbox1" value="layer3" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">poi</p>
      </ul>
      
      <p> <input type="checkbox" name="checkbox_v2" value="version2" onclick="allSelect('checkbox_v2', 'checkbox2')">默認全不選</p>
      <ul>
          <p> <input type="checkbox" name="checkbox2" value="layer1" onclick="singleSelect2parent('checkbox_v2', 'checkbox2')" >tiger_roads</p>
          <p> <input type="checkbox" name="checkbox2" value="layer2" onclick="singleSelect2parent('checkbox_v2', 'checkbox2')">poly_landmarks</p>
          <p> <input type="checkbox" name="checkbox2" value="layer3" onclick="singleSelect2parent('checkbox_v2', 'checkbox2')">poi</p>
      </ul>
      </body>
      
      </html>

      4 總結(jié)
      (1) 數(shù)據(jù)獲取使用request請求,常用數(shù)據(jù)格式有json,form,file及地址欄的get請求數(shù)據(jù);
      (2) form及json數(shù)據(jù)請求方式均有兩種,如request.json.get(),request.json[];
      (3) 獲取文件數(shù)據(jù),可通過filename屬性獲取文件名,save屬性進行保存;
      (4) 地址欄可直接寫入數(shù)據(jù),需在route的方法內(nèi)使用格式為:<type:args>如<int:id>,<string:name>等;
      (5) 通過判斷輸入數(shù)據(jù)的格式,提供不同類型的輸入;

      [參考文獻]
      [1]https://www.jianshu.com/p/ecd97b1c21c1
      [2]https://blog.csdn.net/p571912102/article/details/80526634
      [3]https://www.cnblogs.com/kaituorensheng/p/4529113.html
      [4]https://blog.csdn.net/kuangshp128/article/details/68926902

      來源:https://www./content-4-532651.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多