1. 请求
# request.method
# request.args
# request.form
# request.values
# request.cookies
# request.headers
# request.path
# request.full_path
# request.script_root
# request.url
# request.base_url
# request.url_root
# request.host_url
# request.host
# request.files
# obj = request.files['the_file_name'] #上传文件
# obj.save('/var/www/uploads/' + secure_filename(f.filename)) # 保存文件
2. 响应
响应体:
return “asdf” #字符串
return jsonify({'k1':'v1'}) # json字符串
return render_template('html模板路径')
return redirect('/index.html')
定制响应头:
# 通过make_response 对象来封装响应头和响应体
obj = make_response("响应体内容")
obj.headers['xxxxxxx'] = '123'
obj.set_cookie('key', 'value')
return obj
from flask import Flask, render_template, request, redirect, session, views, make_response, jsonify app = Flask(import_name=__name__) app.secret_key = 'flask' # app.debug = True @app.route('/index') def index(): print(dir(request)) # 请求相关 print(request.method) print(request.args) print(request.form) print(request.values) print(request.cookies) print(request.headers) print(request.path) print(request.full_path) print(request.script_root) print(request.url) print(request.base_url) print(request.url_root) print(request.host_url) print(request.host) # 响应 dict = {'name': 'ok'} # response = make_response("123") response = make_response(jsonify(dict)) response.set_cookie(key="name", value="admin")
response.headers['age'] = 18
return response
if __name__ == '__main__':
app.run()