首頁>技術>

開通阿里雲簡訊服務申請簽名和模板下載pythonSDK

pip install aliyun-python-sdk-core

設定AK和SK封裝發簡訊程式碼utils/BaseView.pyimport jsonfrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.request import CommonRequestfrom syl.settings import ALY_ACCESSKEY_SECRET, ALY_ACCESSKEY_ID# phone = ""# phone_code = ""# data = {"code": phone_code}def send_sms(phone, data):# accessKeyId# accessSecret# cn-hangzhouclient = AcsClient('<ALY_ACCESSKEY_SECRET>', '<ALY_ACCESSKEY_ID>', '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 | httprequest.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'))簡訊傳送和驗證的邏輯# verificationsapp/view.py 傳送介面 ##verificationsapp/VIEW.PYclass 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, image_code_uuid, image_code)# 驗證資料if not all([phone, image_code, image_code_uuid]):return Response({"code": 4005, "msg": "引數不全"})# 邏輯與入庫# 3、驗證圖片驗證碼是否正確# 3.1連線redisredis_cli = get_redis_connection("img_code")# 3.2拿著uuid獲取圖片驗證碼redis_img_code = redis_cli.get(image_code_uuid).decode('utf-8')# 3.3比對redis裡的code和發來的code是否一致if image_code.lower() != redis_img_code.lower():return Response({"code": 4009, "msg": "圖片驗證碼錯誤"})# 4、使用阿里雲傳送簡訊# 4.1準備手機驗證碼number = random.randint(100000, 999999)data = {"code": number}print(data)# 4.2使用封裝好的介面傳送簡訊send_sms(phone, data)# 5.將簡訊驗證碼繫結並存入redisredis_cli.setex(phone, 60 * 60, number)# 6.刪除image_coderedis_cli.delete(image_code_uuid)return Response({"code": 0, "msg": "傳送成功"})後記

近期有很多朋友透過私信諮詢有關Python學習問題。為便於交流,點選藍色自己加入討論解答資源基地

24
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 教你用Python畫了一棵聖誕樹!聖誕節給女朋友製作個小驚喜