Flask 知识点小汇总
不知道而信口开河是不明智的、知道了却闭口不讲是没有尽心竭力。 –佚名
请求方法
因为涉及到了跨域所以必须安装包 flask_cors
- HTTP 请求一共有两种第一种 GET,第二种 POST
默认 HTTP methods 是 GET 可以通过 methods 参数设定,GET 请求
from flask import Flask
from flask import request
from flask_cors import CORS
app = Flask(__name__)
@app.route('/login',methods=['GET'])
def login():
if request.method =='POST':
return '这是Post请求'
else:
return '这是GET请求'
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
- 第二种 POST 请求
from flask import Flask
from flask import request
from flask_cors import CORS
app = Flask(__name__)
@app.route('/login',methods=['GET'])
def login():
if request.method =='POST':
return '这是Post请求'
else:
return '这是GET请求'
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
- 第三种 GET POST 请求
from flask import Flask
from flask import request
from flask_cors import CORS
app = Flask(__name__)
@app.route('/login',methods=['GET','POST'])
def login():
if request.method =='POST':
return '这是Post请求'
else:
return '这是GET请求'
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
获取用户发送的数据
- 利用 request 用户发送过来的数据, request.json,request.args
from flask import Flask
from flask import request
from flask_cors import CORS
from flask import jsonify
app = Flask(__name__)
@app.route('/login',methods=['GET','POST'])
def login():
if request.method =='POST':
#print (request.data)
# print(request.headers)
print(request.json)
userdata = request.json
return '这是POST请求'+ userdata['name']
else:
#发送数据的类型类似 http://127.0.0.1:5000/v1/user/get?name = hello
print(request.headers['auth'])
print("-----------")
print(request.args['name'])
print("--------------")
print(request.args.to_dict()) # 利用to_dict 变成字典
return '这是GET请求' + request.args['name']
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
获取用户通过 URL 传递过来的数据
from flask import Flask
from flask import request
from flask_cors import CORS
from flask import jsonify
app = Flask(__name__)
@app.route('/login/<q>/<isbn>',methods=['GET'])
def login(q,isbn):
if request.method =='GET':
return "用户输入的是"+q +"尾号是"+isbn
else:
return '这是GET请求'
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
- 获取用户上传的图片
from flask import Flask
from flask import request
from flask_cors import CORS
from flask import jsonify
app = Flask(__name__)
@app.route('/login',methods=['GET','POST'])
def login():
if request.method =='POST':
# 文件对象保存在request.files上,并且通过前端的input标签的name属性来获取
fp = request.files.get("f1")
# 保存文件到服务器本地
fp.save("a.jpg")
return '保存成功'
else:
return '这是GET请求'
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
操作 cookies
delete_cookie()
set_cookie()
cookies.get()
from flask import request
from flask import Flask
from flask import Response
from flask_cors import CORS
from datetime import datetime, timedelta
from flask import make_response
app = Flask(__name__)
#获取cookie
@app.route('/get_cookie/')
def get_cookie():
name = request.cookies.get('username')
return name
#删除cookie
@app.route('/del_cookie/')
def del_cookie():
resp = make_response('delete_cookie')
resp.delete_cookie('username')
return resp
#设置cookie
@app.route('/set_cookie/',methods=['GET'])
def set_cookie():
resp = make_response('set_cookie')
resp.set_cookie('username','hahaha', expires=datetime(2026, 4, 19, 14, 0, 0))
return resp
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
返回格式是 json
from flask import request
from flask import Flask
from flask import Response
from flask_cors import CORS
from datetime import datetime, timedelta
from flask import make_response
from flask import jsonify
from flask import json
app = Flask(__name__)
# 路由
@app.route('/returnjson/',methods=['GET','POST'])
def returnjson2():
data = {"a":"b"}
response = make_response(json.dumps(data))
response.headers["Content-Type"] = "application/json"
return response
@app.route('/returnjson/',methods=['GET','POST'])
def returnjson():
data = {"a":"b"}
response = make_response(jsonify(data))
#response.headers["Content-Type"] = "application/json" 这句就可以省略了
return response
if __name__ == "__main__":
CORS(app,supports_credentials=True)
app.run()
操作配置文件
有的时候我们想获取到或者使用 app.config 里面的配置文件
这个时候必须使用 flask 下面的 current_app
from flask import current_app
print(current_app.config['SETTING_PAGE'])