• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共46篇

    python3.X - Web - Flask1.1.1

关闭

返回栏目

关闭

返回python3.X - Web - Flask1.1.1栏目

16 - Flask - [GET][POST]获得参数

作者:

贺及楼

成为作者

更新日期:2024-05-18 12:24:01

Flask - [GET][POST]获得参数

作用:http[GET][POST]获得参数

[GET]2种方法,url传输?、json传输””:””

  1. url:
  2. 域名.com/xx?value=4
  3. value = request.args['value']
  4. value = request.args.get('value')
  1. json传输"":""
  2. $.ajax({
  3. url:"/",
  4. type:'get',
  5. dataType:"text",
  6. data:{
  7. x:{ 'id':'02' },
  8. //发送的东西
  9. },
  10. success:function(data){
  11. //成功之后
  12. },
  13. error:function(jqXHR){
  14. alert("发生错误:"+ jqXHR.status);//错误
  15. }
  16. })
  17. data = request.args['x']#获得数据
  18. data1 = json.loads(data)#json读取
  19. id = data1["id"]
  20. name = data1["name"]

[POST]2种方法,json传输””:””,XML

  1. json传输"":""
  2. $.ajax({
  3. url:"/",
  4. type:'post',
  5. dataType:"text",
  6. data:{
  7. "title":"1",
  8. //发送的东西
  9. },
  10. success:function(data){
  11. //成功之后
  12. },
  13. error:function(jqXHR){
  14. alert("发生错误:"+ jqXHR.status);//错误
  15. }
  16. })
  17. #获得参数
  18. value = request.json['value']
  19. value = request.json.get('value')
  1. # 接收body raw
  2. {
  3. "data":["a","b","c"]
  4. }
  5. import json
  6. from flask import Flask, jsonify, request
  7. @xxx.route("/xxx", methods=['POST'])
  8. def xxx():
  9. try:
  10. data = request.data
  11. data = json.loads(data)
  12. doc = data['data']
  13. print(doc)
  14. return "ok"
  15. except Exception as e:
  16. print(e)
  17. return "fail"
  1. # 接收XML
  2. #获得参数
  3. data = request.data
  4. print(data)
  5. dict = xml_to_dict2(data)
  6. # 处理返回预付单方法
  7. def xml_to_dict2(data):
  8. import xml.etree.ElementTree as ET
  9. xml_dict = {}
  10. data_dic = ET.fromstring(data)
  11. for item in data_dic:
  12. xml_dict[item.tag]=item.text
  13. return xml_dict

暴力测试1

  1. print(request.__dict__) # 直接打印request
  2. # 这是url
  3. # xxx.com?xx=1&aa=2&bb=3
  4. value1 = request.args['value']
  5. value2 = request.args.get('value')
  6. print("value1")
  7. print(value1)
  8. print("value2")
  9. print(value2)
  10. # 这是body的from-data形式有:KEY、VALUE、DESCRIPTION
  11. value3 = request.json['value']
  12. value4 = request.json.get('value')
  13. print("value3")
  14. print(value3)
  15. print("value4")
  16. print(value4)
  17. # 这是body的raw的json形式
  18. # {"AA":"aa","BB":"bb"}
  19. value5 = request.from['value']
  20. value6 = request.from.get('value')
  21. print("value5")
  22. print(value5)
  23. print("value6")
  24. print(value6)
  25. data = request.args['x']#获得数据
  26. data1 = json.loads(data)#json读取
  27. id = data1["id"]
  28. name = data1["name"]

暴力测试2

  1. print("1:request.form")
  2. print(request.form)
  3. print("2:request.data")
  4. print(request.data)
  5. print("3:request.method")
  6. print(request.method)
  7. print("4:request.values")
  8. print(request.values)
  9. print("5:request.headers")
  10. print(request.headers)
  11. print("6:request.args")
  12. print(request.args)
  13. print("7:request.json")
  14. print(request.json)