最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask中用redis保存和管理验证码

Flask crifan 4857浏览 0评论

折腾:

【记录】Flask管理短信验证码

期间,需要去

尝试使用redis,去存储验证码

管理验证码:加上过期时间,用于后期验证是否过期而无效

flask开发restful api系列(5)-短信验证码 – 月儿弯弯0204 – 博客园

登陆验证手机短信验证码方案 – SegmentFault

搜:

Flask redis 扩展

参考:

pip安装flask-redis后,如何启动redis-server服务? – Redis – 知乎

Flask redis 保存键值对

redis_store

python redis_store

flask redis_store

Server-side Sessions with Redis | Flask (A Python Microframework)

Flask-Session — Flask-Session 0.3.0 documentation

-》好像是:

Flask的session,底层支持各种存储,包括redis,以及mongodb,memcache等

Flask-And-Redis — Flask-And-Redis 0.6 documentation

GitHub – underyx/flask-redis: A Flask extension for using Redis

-》这里有之前找的redis_store

GitHub – andymccurdy/redis-py: Redis Python Client

->有点点搞懂了:

-》flask-redis是基于redis-py的

-》要用redis之前,需要先安装:redis-py

-》redis-py,就是:pip install redis中redis

-》另外,这些都只是python的redis的客户端,还需要去在服务器上安装redis服务器端,需要去参考官网:

Redis

去安装。

不过由于之前已经折腾过一些reis的东西了。

所以去参考之前的:

[已解决]Flask中添加后台进程用于提醒到期时发送通知

-》其实可以直接pip去安装flask-redis,其会自动安装所依赖的redis的库的

->然后去安装flask-redis

(RunningFast) ➜  RunningFast pip install flask-redis
Collecting flask-redis
  Using cached Flask_Redis-0.3.0-py2.py3-none-any.whl
Requirement already satisfied (use –upgrade to upgrade): Flask>=0.8 in /root/Envs/RunningFast/lib/python2.7/site-packages (from flask-redis)
Collecting redis>=2.7.6 (from flask-redis)
  Using cached redis-2.10.5-py2.py3-none-any.whl
Requirement already satisfied (use –upgrade to upgrade): itsdangerous>=0.21 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.8->flask-redis)
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.8->flask-redis)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.8->flask-redis)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask>=0.8->flask-redis)
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/RunningFast/lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.8->flask-redis)
Installing collected packages: redis, flask-redis
Successfully installed flask-redis-0.3.0 redis-2.10.5

果然自动帮忙安装了redis库:

Successfully installed flask-redis-0.3.0 redis-2.10.5

去参考:

[已解决]为Flask的后台任务而在CentOS中安装Redis

想要用:

redis-server /etc/redis/redis.conf

启动redis服务器,不过发现之前已经启动过了:

(RunningFast) ➜  RunningFast ll /etc/redis                     
total 48K
-rw-r–r– 1 root root 46K Sep  3 21:59 redis.conf
(RunningFast) ➜  RunningFast redis-server /etc/redis/redis.conf
14291:M 08 Oct 15:08:02.983 # Creating Server TCP listening socket 127.0.0.1:6379: bind: Address already in use
(RunningFast) ➜  RunningFast netstat -tulpn | grep 6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      19659/redis-server  

然后参考官网:

GitHub – underyx/flask-redis: A Flask extension for using Redis

去写代码

看到有说:REDIS_URL

不过之前的项目好像也没去设置REDIS_URL也是可以的。

【已解决】Flask中如何使用flask-redis去保存读取验证数据

【总结】

此处,在之前安装好redis的server的基础上,去:

redis-server /etc/redis/redis.conf

确保运行了redis的服务器。

然后再去Python和flask的虚拟环境中安装flask-redis:

pip install flask-redis

其会自动安装所依赖的库:redis(即redis-py)

然后代码中:

from flask import Flask
from flask_redis import FlaskRedis
app = Flask(__name__, instance_relative_config=True)
redis_store = FlaskRedis(app)
        savedPhoneSmsCode = redis_store.set(phone, smsCodeStr)
        gLog.debug(“savedPhoneSmsCode=%s”, savedPhoneSmsCode)
        setExpireSmsCode = redis_store.expire(phone, 60)
        gLog.debug(“setExpireSmsCode=%s”, setExpireSmsCode)
        readbackSmsCode = redis_store.get(phone)
        gLog.debug(“readbackSmsCode=%s”, readbackSmsCode)

即可实现:

保存手机号对应的验证码,且设置了过期时间

之后即可通过改手机号,能否读取到验证码,而判断验证码是否过期了。

转载请注明:在路上 » 【已解决】Flask中用redis保存和管理验证码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.194 seconds, using 22.13MB memory