03-06flask 信息誊写

2019-03-06  本文已影响0人  生命的怒放
import sys

from flask import Flask, request
from flask_script import Manager

# 生成Flask对象,默认进行一些配置,包括静态文件、模板文件的配置
app = Flask(__name__)

@app.route('/')  # 括号里的称路由
def hello():
    # 127.0.0.1:5000/
    return 'Hello World'

@app.route('/hello/')
def hello_girl():
    # 127.0.0.1:5000/hello/
    return 'hello 妹子'
# 匹配路由   <类型(转换器):参数名>
@app.route('/day01/<int:id>/')
def hello_day01(id):
    return 'hello day01 %s' % id

@app.route('/name/<string:name>/')
def hello_name(name):
    return 'hello %s' % name

@app.route('/sname/<name>/')
def hello_sname(name):
    print(type(name))
    return 'hello sname: %s' % name

@app.route('/float/<float:num>/')
def hello_float(num):
    return 'hello float: %s' % num


@app.route('/params/')
def params():

    # 获取get请求传递的参数
    name = request.args['name']
    # name = request.args.get('name')
    pwd = request.args.get('pwd')
    return '获取get参数: name=%s pwd=%s' % (name, pwd)
 # http://127.0.0.1/params/?name=hehe&pwd=20


@app.route('/post_params/', methods=['POST'])
def post_params():
    name = request.form['name']
    # name = request.form.get('name')
    age = request.form.get('age', 18)
    return '获取post参数: name=%s age=%s' % (name, age)


manage = Manager(app)
# 直接执行本hello.py文件时,才执行if操作
if __name__ == '__main__':
    # 启动web服务
    # app.run(host='0.0.0.0', port=80, debug=True)

    # 使用Manager管理启动命令
    manage.run()

第二天

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)
    # 响应状态码一般不填

    # 响应.浏览器网址打开,make_response响应,返回后面的内容,显示在浏览器上
    # 与直接写return ‘今天天气不好’没什么区别
    # return make_response('<h2>今天天气不好</h2>')

    # render_template() 打开templates文件夹中的html文件
    # render_template的功能是对先引入index.html,同时根据后面传入的参数,对html进行修改渲染。
    index = render_template('index.html')
    return make_response(index, 200)


@app.route('/register/', methods=['GET', 'POST'])
def register():
    print(request.method)
    # 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()
上一篇下一篇

猜你喜欢

热点阅读