• 阿里云短信


    开通阿里云短信服务

    申请签名和模板

    img

    下载pythonSDK

    pip install aliyun-python-sdk-core

    设置AK和SK

    img

    发送短信代码

    -- coding: utf-8 --

    import json

    from aliyunsdkcore.client import AcsClient

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.request import CommonRequest
    def soum_code(phone,data):
    client = AcsClient('LTAI4G53mfmtK93EMJTwfFRy', 'nLpRi04FJknXZRYNOJVhmSDj02aEN1', 'cn-hangzhou')
    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', "ABC商城")
    request.add_query_param('TemplateCode', "SMS_205397853")
    request.add_query_param('TemplateParam', data)
    
    response = client.do_action(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    ser=json.loads(str(response, encoding='utf-8'))
    if ser['Code']=='OK':
        return True
    else:
        return False
    

    将其放入MyBaseView中进行封装

    image-20201104081029349

    class sms_codes(APIView):
    def post(self,request):
    phone=request.data.get('phone')
    print(phone)
    data=random.randint(100000,999999)
    date={'code':data}
    ser=soum_code(phone,date)
    print(ser)
    if ser:
    return Response({'msg':'发送','code':200})
    else:
    return Response({'msg':'shibai'})

  • 相关阅读:
    【LeetCode-回溯】组合总和
    MongoDB复制集成员类型
    Vant中的日期元素在iOS上显示NaN
    Vue风格
    Git设置代理和取消代理的方式
    吴晓波——疫情下的的“危”与“机”
    Vant库在PC端的使用
    买保险,不上当
    Vant的引入方式
    Duplicate keys detected: 'xxx'. This may cause an update error.
  • 原文地址:https://www.cnblogs.com/li-xiao-yang/p/13924041.html
Copyright © 2020-2023  润新知