python跳板机需要实现登录短信验证功能,怎么做

2021-04-20 14:39发布

2条回答
studentaaa
2021-04-25 22:30

第一步: 下载python-redis模块


pip install python-redis


第二步: 配置setting.py中写配置


配置Redis为python的缓存,替换原来的session


1. #配置Redis为Django缓存

2. CACHES = {

3. "default": {

4. "BACKEND": "django_redis.cache.RedisCache",

5. "LOCATION": "redis://127.0.0.1:6379/0", #地址

6. "OPTIONS": {

7. "CLIENT_CLASS": "django_redis.client.DefaultClient",

8.  }

9.  }

10. }

11. # 将session缓存在Redis中

12. SESSION_ENGINE = "django.contrib.sessions.backends.cache"

13. SESSION_CACHE_ALIAS = "default"

14. # session 设置(可以不写)

15. SESSION_COOKIE_AGE = 60 * 60 * 12 # 12小时

16. SESSION_SAVE_EVERY_REQUEST = True

17. SESSION_EXPIRE_AT_BROWSER_CLOSE = True # 关闭浏览器,则COOKIE失效


第三步: views导入缓存cache模块


from django.core.cache import cache

1

第四步: 使用


1. def test_redis(request):

2.  # 存储数据

3.  chache.set("name", "tom", 20) # 该值的有效期为20s

4.   # 判断Redis中是否存在

5.  print(cache.has_kay("name"))  # 包含: true

6.   # 获取

7.    print(cache.get("name"))  # 返回: tom  无返回null

8.  return HttpResponse("测试Redis")


功能2: 短信下发

第一步: 申请短信服务

第二步: 独立发送短信和生成验证码的模块


#!/usr/bin/env python

#coding=utf-8

import random

from aliyunsdkcore.client import AcsClient

from aliyunsdkcore.request import CommonRequest

from utils import restful

def send_sms(phone,code):

    client = AcsClient('mxTYXZ4QDQecJQDN', 'znxNezmm4zfA9k

    Pyqx1WrpznjCaJFT', 'cnhangzhou')

    #phone = '17600950805'

    #aa= '222222'

    code = "{'code':%s}"%(code)

    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_162738723')

    request.add_query_param('TemplateParam',code )

    response = client.do_action(request)

    # python2: print(response)

    print(str(response, encoding = 'utf-8'))

    return str(response, encoding = 'utf-8')

#数字表示生成几位, True表示生成带有字母的 False不带字母的

def get_code(n=6,alpha=True):

    s = '' # 创建字符串变量,存储生成的验证码

    for i in range(n): # 通过for循环控制验证码位数

            num = random.randint(0,9) # 生成随机数字0-9

            if alpha: # 需要字母验证码,不用传参,如果不需要字母的,

            关键字alpha=False

                upper_alpha = chr(random.randint(65,90))

                lower_alpha = chr(random.randint(97,122))

                num = random.choice([num,upper_alpha,lower_alpha])

            s = s + str(num)

    return s

if __name__ == '__main__':

    send_sms('18434288349', get_code(6,False))

    print(get_code(6,False)) # 打印6位数字验证码

    print(get_code(6,True)) # 打印6位数字字母混合验证码

    print(get_code(4,False)) # 打印4位数字验证码

    print(get_code(4,True)) # 打印4位数字字母混合验证码


功能3: 后台功能: 发送短信接口

流程:

获取手机号---->生成6位验证码–>缓存验证码到Redis—>发短信–>返回状态


# 发短信接口

def sms_send(request):

    # http://localhost:8000/duanxin/duanxin/sms_send/?phone=18434288349

    # 1 获取手机号

    phone = request.GET.get('phone')

    # 2 生成6位验证码

    code = aliyunsms.get_code(6, False)

    # 3 缓存到Redis

    cache.set(phone,code,60) #60s有效期

    print('判断缓存中是否有:',cache.has_key(phone))

    print('获取Redis验证码:',cache.get(phone))

    # 4 发短信

    result = aliyunsms.send_sms(phone, code)

    return HttpResponse(result)


功能4: 短信验证码校验

流程:

获取前台电话和验证码----> 获取Redis中存的验证码—>对比是否相等–>返回结果


# 短信验证码校验

def sms_check(request):

    # /duanxin/sms_check/?phone=xxx&code=xxx

    # 1. 电话和手动输入的验证码

    phone = request.GET.get('phone')

    code = request.GET.get('code')

    # 2. 获取redis中保存的code

    print('缓存中是否包含:',cache.has_key(phone))

    print('取值:',cache.get(phone))

    cache_code = cache.get(phone)

    # 3. 判断

    if code == cache_code:

        return HttpResponse(json.dumps({'result':'OK'}))

    else:

        return HttpResponse(json.dumps({'result':'False'}))


手动在浏览器上给假设的参数进行测试:


http://localhost:8000/duanxin/sms_send/?phone=手机号

http://localhost:8000/duanxin/sms_check/?phone=手机号&code=验证码


功能5: 统一接口的数据格式:


参考:


https://blog.csdn.net/xyy1028/article/details/84981627

https://www.runoob.com/w3cnote/restful-architecture.html


统一的接口模块restful.py


#encoding: utf-8

from django.http import JsonResponse

class HttpCode(object):

    ok = 200

    pageerror = 404

    methoderror = 405

    servererror = 500

# {"code":400,"message":"","data":{}}

def result(code=HttpCode.ok,message="",data=None,kwargs=None):

    json_dict = {"code":code,"message":message,"result":data}

    if kwargs and isinstance(kwargs,dict) and kwargs.keys():

        json_dict.update(kwargs)

    return JsonResponse(json_dict,json_dumps_params={'ensure_ascii': False})

def ok(message='OK',data=None):

    return result(code=HttpCode.ok,message=message,data=data)

def page_error(message="",data=None):

    return result(code=HttpCode.pageerror,message=message,data=data)

def method_error(message='',data=None):

    return result(code=HttpCode.methoderror,message=message,data=data)

def server_error(message='',data=None):

    return result(code=HttpCode.servererror,message=message,data=data)


任何接口的返回结果,都是用resutful.py方法进行规整


# 短信验证码校验

def sms_check(request):

    # /duanxin/sms_check/?phone=xxx&code=xxx

    # 1. 电话和手动输入的验证码

    phone = request.GET.get('phone')

    code = request.GET.get('code')

    # 2. 获取redis中保存的code

    print('缓存中是否包含:',cache.has_key(phone))

    print('取值:',cache.get(phone))

    cache_code = cache.get(phone)

    # 3. 判断

    if code == cache_code:

        #格式统一调整后的

        return restful.ok("OK",data=None)

    else:

        #格式统一调整后的

        return restful.params_error("验证码错误", data=None)

一周热门 更多>