[TOC]
request
from flask import Flask
from flask import request
app = Flask(__name__)
'''
获取当前请求的内容
1 先要导入request
2 直接用request.方法,属性
'''
@app.route('/login.html', methods=['GET', "POST"])
def login():
# 请求相关信息
# request.method 提交的方法
print("request.method",request.method)
# request.args get请求提及的数据
print("request.args", request.args)
# request.form post请求提交的数据
# request.values post和get提交的数据总和
# request.cookies 客户端所带的cookie
# request.headers 请求头
# request.path 不带域名,请求路径
# request.full_path 不带域名,带参数的请求路径
# request.script_root
# request.url 带域名带参数的请求路径
# request.base_url 带域名请求路径
# request.url_root 域名
# request.host_url 域名
# request.host 127.0.0.1:500
# request.files
# obj = request.files['the_file_name']
# obj.save('/var/www/uploads/' + secure_filename(f.filename))
return "字符串"
if __name__ == '__main__':
app.run()
response
from flask import Flask
from flask import render_template, redirect, make_response, jsonify
app = Flask(__name__)
@app.route('/login.html', methods=['GET', "POST"])
def login():
# 响应相关信息
# return "字符串" # 字符串
# return render_template('html模板路径',**{}) # 渲染模板
# return redirect('/index.html') # 重定向
#return jsonify({'k1':'v1'}) # 返回json数据
"""
返回的时候,如果需要设置额外的响应参数,比如cookie,heard
1 response=make_response(四剑客)
2 response.设置属性=“属性值”
3 return response
"""
# response = make_response(render_template('index.html'))
# response是flask.wrappers.Response类型
# response.delete_cookie('key') # 删除cookies
# response.set_cookie('key', 'value') # 设置cookies
# response.headers['X-Something'] = 'A value' # 设置请求头
# return response
response1=make_response(render_template('rr.html'))
#response1.set_cookie('key_sss', 'valuessbbsd')
# response1.delete_cookie('key_sss')
response1.headers['sb'] = 'asdas'
return response1
if __name__ == '__main__':
app.run()