day02 请求和响应

GET请求:获取参数:request.args , request.args.get('key')
post请求获取参数:request.form , request.get(key) 或者 request.form[key]
获取请求路径:request.path
请求cookies:request.cookies
获取上传文件:request.files

import redis
from flask import Flask, make_response, render_template,
request, session
from flask_script import Manager
from flask_session import Session

app = Flask(name)

@app.route('/')
def hello():
return 'hello world'

@app.route('/make_res/')
def make_res():
# make_response('响应内容', 响应状态码)创建响应对象
# return make_response('hello flask day02', 200)
# return make_response('

今天天气不好

')
index = render_template('index.html')
return make_response(index, 200)

@app.route('/register/', methods=['GET', 'POST'])
def register():
print(request.method)
if request.method == 'GET':
return render_template('register.html')
if request.method == 'POST':
# 模拟注册功能
# 1. 获取页面中传递的参数
username = request.form.get('username')
password = request.form.get('password')
password2 = request.form.get('password2')
# 模拟注册
if username == 'coco' and password == password2
and password == '123456':
# 返回登录页面

        return render_template('login.html')
    else:
        return render_template('register.html')

@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')

if request.method == 'POST':
    # 1. 获取参数
    username = request.form.get('username')
    password = request.form.get('password')
    # 2. 模拟登陆
    if username == 'coco' and password == '123456':
        # 向cookie中设置参数
        res = make_response(render_template('index.html'))
        # 设置cookie
        res.set_cookie('token', '12345678', max_age=3000)
        # res.set_cookie('name', 'coco', max_age=3000)
        return res
    else:
        return render_template('login.html')

@app.route('/logout/')
def my_logout():
# 注销
res = make_response(render_template('login.html'))
res.delete_cookie('token')
return res

@app.route('/index/')
def index():
# 登录过后能看到index.html页面,没有登录跳转到登录页面
token = request.cookies.get('token')
name = request.cookies.get('name')
if token == '12345678':
# 判断登录成功了
return render_template('index.html', name=name)
else:
# 判断登录失败了
return render_template('login.html')

@app.route('/session_login/', methods=['GET', 'POST'])
def session_login():
if request.method == 'GET':
return render_template('session_login.html')

if request.method == 'POST':
    # 解析参数
    username = request.form.get('username')
    password = request.form.get('password')
    if username == 'coco' and password == '123456':
        # 向session会话中设置键值对
        session['login_status'] = 1
        # session['name'] = 'coco'
        # session['pwd'] = '123456'
        return render_template('index.html')
    else:
        return render_template('session_login.html')

@app.route('/session_index/')
def session_index():
if 'login_status' in session:
return render_template('index.html')
else:
return render_template('session_login.html')

@app.route('/session_logout/')
def session_my_logout():
# 注销
del session['login_status']
return render_template('session_login.html')

加密,加密复杂程度和设值有关

app.secret_key = '1234567890'

配置session信息

from flask_session import Session

app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host='127.0.0.1', port=6379)
Session(app)

manage = Manager(app)

if name == 'main':
# app.run()
manage.run()

你可能感兴趣的:(day02 请求和响应)