当前位置:网站首页>Flask request data and get response
Flask request data and get response
2022-07-20 12:16:00 【Wbig】
Request data and get response
1. Request data and its acquisition
1.1 Request relevant data
The data related to the request is the parameters taken when sending
The way | explain |
---|---|
Fixed parameter | Parameters in the route , Parameters that must be taken when sending a request |
character string :args | Route followed by string, not essential |
Forms :form | Equivalent to sending post request |
Upload files :file | Send a request to the server , Upload files |
Request header :headers | The request header carries the request information |
Request method :method | The request method used by the request |
URL Address | request URL Address |
1.2 Fixed parameters and converters
1.2.1 Fixed parameter
Fixed parameter : Referring to URL Fixed in , Is an inaccessible part , Different from query string , Query strings are optional .
@app.route('/hello/<int:id>/<name>')
def hello(id,name):
return " The received ID yes :%s,name yes %s"%(id,name)
1.2.2 converter
The converter is to make a string meet a regx Properties of , Make regular judgments
Custom converter can meet the required functions
# 1. Import converter base class
from werkzeug.routing import BaseConverter
# 2. Inherit the converter base class and implement regex Method
class PhoneConverter(BaseConverter):
regex = r'1[3-9][0-9]{9}'
# 3. Mount the custom converter to flask On the object
app.url_map.converters["phone"] = PhoneConverter
# Use custom Converter
@app.route('/phone/<phone:my_phone>')
def get_phone(my_phone):
return " The current mobile number is :%s"%my_phone
1.3 Acquisition of query parameters
# Acquisition of query parameters
# 1. Import request object
from flask import request
@app.route('/query')
def query():
# Convert the immutable dictionary type to normal dict type
params = request.args.to_dict()
print(params)
print(type(params))
name = request.args.get("name")
age = request.args.get("age")
return " The current name is %s, Age is %s"%(name,age)
1.4 Query form data acquisition
from flask import request
@app.route("/form",methods=["post"])
def form():
name = request.form.get("name")
age = request.form.get("age")
return " The name is %s, Age is %s"%(name,age)
1.5 Upload files
from flask import request
@app.route("/form",methods=["post"])
def form():
name = request.form.get("name")
age = request.form.get("age")
return " The name is %s, Age is %s"%(name,age)
@app.route("/upload",methods=["post"])
def upload():
img = request.files.get("img")
print(img)
img.save("./static/01.png")
return " Upload files "
2. The response data
2.1 Response return HTML page
from flask import render_template
@app.route('/page')
def page():
return render_template("hello.html")
2.2 Redirect jump page
# Redirect jump page
from flask import redirect
@app.route('/jump')
def jump():
return redirect("https://www.baidu.com/")
2.3 return json data
from flask import jsonify
@app.route("/return_json")
def return_json():
data = {
"name" : "zhangsan",
"age" : 18,
"action":[" eat "," sleep "," Use the toilet "]
}
return jsonify(data)
2.4 Return tuple data
from flask import jsonify
@app.route("/return_tuple")
def return_tuple():
return ("hello",400,{
"aaaa":"bbbb"
})
3. Custom response
from flask import make_response
@app.route("/return_obj")
def return_obj():
resp = make_response("hello")
resp.status = "404"
resp.headers["aaa"] = "bbb"
return resp
边栏推荐
猜你喜欢
DOM系列之排他思想
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
Matlab finite element calculation
DOM系列之元素的属性操作
Basic knowledge points of reptiles (1)
查询oracle11g日志的办法,数据库审计,记录排查
20220710 leetcode week: move the clip to get the string
MySql索引类型和高性能索引学习总结
全新出品!阿里 P5 工程师~P8 架构师晋升路线揭秘
How much is the price of the moving ring monitoring system
随机推荐
Object. Defineproperty, callback function, array method
GameFi 行业下滑但未出局| June Report
Resolved (selenium reports an error) attributeerror: 'webdriver' object has no attribute 'execute_ cdp_ cmd‘
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
SylixOS TCP 数据段发送流程简述
三层交换技术
Synoxos TCP data segment sending process
基于 MATLAB 的圆柱度误差评定方法
Reptile exercises (II)
Chrome Basics
[wechat applet] from form (88/100)
1381:城市路(Dijkstra)
HCIA-R&S自用笔记(12)路由基础、直连路由与静态路由
Relationship extraction onerel
什么是反向代理?
DOM系列之DOM介绍及获取
Chrome基础
Simulation of PID tuning method in flow control system
Matlab finite element calculation
1381: City Road (Dijkstra)