flask连接redis

1 篇文章 0 订阅
1 篇文章 0 订阅
from flask import Flask, render_template, flash, redirect, url_for,session, logging, request
# from data import Articles
# from flask_mysqldb import MySQL
# from wtforms import Form, StringField, TextAreaField, PasswordField, validators
# from passlib.hash import sha256_crypt
# from functools import wraps
import redis
from flask_session import Session
import pickle
import json
import re
from flask_bootstrap import Bootstrap

app = Flask(__name__)
bootstrap = Bootstrap(app)


class Redis:
    @staticmethod
    def connect():
        r = redis.StrictRedis(host='localhost', port=6379)
        return r

    #将内存数据二进制通过序列号转为文本流,再存入redis
    @staticmethod
    def set_data(r,key,data,ex=None):
        r.set(key,pickle.dumps(data),ex)

    # 将文本流从redis中读取并反序列化,返回返回
    @staticmethod
    def get_data(r,key):
        data = r.get(key)
        if data is None:
            return None
        return pickle.loads(data)

@app.route('/<key>', methods=['GET', 'POST'])
# @cache.cached(key_prefix=cache_key)
def login(key):
	# L_phone = []
	cached_redis_remark = ""
	r = Redis.connect()
	Redis.set_data(r, '18333608826', '20190910')
	Redis.set_data(r, '18333608826', '201909')
	times = Redis.get_data(r, '%s' % key)
	# phone = session.hscan_iter('hash1')
	if request.method == 'POST' and times:
		json.dumps(key)
		json.dumps(times)
		return  render_template('index.html', key = key, times = times)
	else:
		msg = "check your phonenumber is correct!"
		# session['username'] = select
		return  render_template('index.html', key = key, msg = msg)


if __name__ == '__main__':
    app.run(debug=False)


# can add debug=True in order to start in development mode

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值