图片验证码的生成
1.django 缓存设置
1.1 安装Django缓存模块
pip install django-redis==4.12.1
1.2 syl/settings.py
中配置缓存
# 缓存配置
CACHES = {
# django存缓默认位置,redis 0号库
# default: 连接名称
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
# django session存 reidis 1 号库(现在基本不需要使用)
"session": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
# 图形验证码,存redis 2号库
"img_code": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/2",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
# 配置session使用redis存储
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
# 配置session存储的位置: 使用cache中的 session配置
SESSION_CACHE_ALIAS = "session"
2.新建应用verifications
- 图形验证码
- 短信验证码
- 邮件验证
'''2.1 在apps文件夹下新建应用: verifications'''
python ../manage.py startapp verifications # 切换到apps文件夹下执行创建命令
'''2.2 在syl/settings.py中添加应用'''
INSTALLED_APPS = [
'verifications.apps.VerificationsConfig',
]
'''2.3 在syl/urls.py主路由中添加'''
path('verify/', include('verifications.urls')),
'''2.4 添加子路由: verifications/urls.py'''
from django.urls import path
from . import views
urlpatterns = [
# path('image_codes/', views.ImageCodeView.as_view())
]
3.图形验证码captcha使用
1.下载captcha压缩包captcha.zip,放到项目packages文件夹下
2.解压captcha.zip放到syl/libs文件夹下
3.解压文件中的syl/libs/captcha/captcha.py 右键运行即可生成图片验证码
unzip xxx.zip
4.在verifications/views.py中使用
from django.http import HttpResponse, HttpResponseForbidden
from django.views import View
from django_redis import get_redis_connection
from libs.captcha.captcha import captcha
class ImageCodeView(View):
def get(self, request):
# 1.接收数据(uuid)
uuid = request.GET.get('uuid')
# 2.校验数据
if not uuid:
return HttpResponseForbidden('uuid无效')
# 3.处理业务
# 获取图片文本内容和图片二进制代码
text, image = captcha.generate_captcha() # text=GPMZ; image=图片
# 4.把uuid和图片文本存入redis
redis_client = get_redis_connection('img_code') # 获取redis客户端
# 5.写入redis(是字符串)
redis_client.setex(uuid, 60 * 5, text)
# 6.返回响应图片
return HttpResponse(image, content_type='image/jpg')
5.测试验证码接口
http://192.168.56.100:8888/verify/image_codes/?uuid=66ea64aa-fbe6-11ea-a3d3-005056c00008
127.0.0.1:6379># select 2
OK
127.0.0.1:6379[2]># keys *
1) "66ea64aa-fbe6-11ea-a3d3-005056c00008"
127.0.0.1:6379[2]># get 66ea64aa-fbe6-11ea-a3d3-005056c00008
"JEZ6"
阿里云短信服务的使用
申请短信服务,领取免费短信,申请签名和模板
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ervjy6rw-1608810993222)(/Users/zhangpengpeng/jiyunedu/md文档汇总/小实训课件/2002B授课笔记/pic/短信领取.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zoe4KFzG-1608810993226)(/Users/zhangpengpeng/jiyunedu/md文档汇总/小实训课件/2002B授课笔记/pic/签名和模板.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sD7ZApwS-1608810993228)(/Users/zhangpengpeng/jiyunedu/md文档汇总/小实训课件/2002B授课笔记/pic/添加签名和模板.png)]
- 封装发短信代码utils/MyBaseView.py
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from code2001B.settings import ALY_ACCESSKEY_ID,ALY_ACCESSKEY_SECRET,QINIU_ID,QINIU_SECRET,SHAZFB_APPID,SHA_RETURN_URL,SHA_URL
def send_sms(phone,data):
client = AcsClient(ALY_ACCESSKEY_ID, ALY_ACCESSKEY_SECRET, 'cn-hangzhou')
#data ={ "code" : 123456 }
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', phone)
request.add_query_param('SignName', "美多商城")
request.add_query_param('TemplateCode', "SMS_185212884")
request.add_query_param('TemplateParam', data)
response1 = client.do_action(request)
# python2: print(response)
res = json.loads(str(response1, encoding='utf-8'))
- 短信发送和验证的逻辑
from rest_framework.views import APIView
from rest_framework.response import Response
from libs.captcha.captcha import captcha
from django_redis import get_redis_connection
from django.http.response import HttpResponse
import random
from verificationsapp.tasks import send_sms
from django_redis import get_redis_connection
class SendSMSCode(APIView):
def post(self,request):
phone = request.data.get("phone")
image_code = request.data.get("image_code")
image_code_uuid = request.data.get("image_code_uuid")
print(phone)
print(image_code_uuid)
print(image_code)
if not all([phone,image_code,image_code_uuid]):
return Response({"code":4005,"msg":"参数不全"})
#先获取redis 里的图片验证码来比对
redis_cli = get_redis_connection("img_code")
redis_img_code = redis_cli.get(image_code_uuid).decode()
print(redis_img_code)
print(image_code)
if image_code.lower() != redis_img_code.lower():
return Response({"code":4003,"msg":"参数错误"})
#发送短信
num = random.randint(100000,999999)
print(num)
send_data = {"code":10086}
# send_sms.delay(phone,send_data)
# 删除redis里的image_code,保存phone_code
#pipeline管道:作用就是把多个命令放在一起来执行
pl = redis_cli.pipeline()
pl.setex(phone,60*5,num)
pl.delete(image_code_uuid)
pl.execute()
return Response({"code":0,"msg":"发送成功"})
pipeline原理
1.1 redis发送数据原理
-
Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互。
-
一般来说客户端从提交请求到得到服务器相应,需要传送两个tcp报文。
-
设想这样的一个场景,你要批量的执行一系列redis命令,例如执行100次get key,这时你要向redis请求100次+获取响应100次。如果能一次性将100个请求提交给redis server,执行完成之后批量的获取相应,只需要向redis请求1次,然后批量执行完命令,一次性结果,性能是不是会好很多呢?
1.2 未使用pipeline执行N条命令
1.3 使用了pipeline执行N条命令