Implementing SMS Verification with Tencent Cloud SDK in Python
This tutorial explains how to set up a Python environment, configure a Tencent Cloud SMS application, obtain the required appid and appkey, create message templates and signatures, and implement a reusable Captcha class that generates verification codes and sends them via the Tencent Cloud SDK.
Many web and mobile applications require phone number verification via SMS; this guide shows how to implement that functionality using Python and the Tencent Cloud SMS service.
Prerequisites
Operating System: macOS
Python 3.6.2
Tencent Cloud personal SMS service (free 100 messages per month)
First, install the Tencent Cloud SDK: pip3 install qcloudsms_py After creating an SMS application on the Tencent Cloud console, note the appid and appkey , then apply for a message signature and a template. The template may contain placeholders such as {1} that will be replaced with actual values when sending.
Code implementation
The following class encapsulates the required operations:
def create_captcha(self):
"""create and return captcha
:return: A six-digit verification code
"""
captcha = ''
for i in range(6):
now_number = str(random.randint(0, 9))
captcha += now_number
return captcha def create_ssender(self):
"""create ssender object
:return: a ssender object
"""
appid = ******
appkey = '******'
ssender = SmsSingleSender(appid, appkey)
return ssender def send_short_message(self, phone_number):
"""Send Verification Code Short Message
:param phone_number: Short Message Receiving Number
:return: a status code
"""
ssender = self.create_ssender()
params = []
captcha = self.create_captcha()
defult_time = "2"
params.append(captcha)
params.append(defult_time)
template_id = ******
sms_sign = '******'
try:
result = ssender.send_with_param(86, phone_number, template_id, params, sign=sms_sign, extend="", ext="")
return result['result']
except HTTPError as e:
print(e)
except Exception as e:
print(e)The complete source code combines these methods into a Captcha class:
import random
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
class Captcha():
def create_captcha(self):
"""create and return captcha
:return: A six-digit verification code
"""
captcha = ''
for i in range(6):
now_number = str(random.randint(0, 9))
captcha += now_number
return captcha
def create_ssender(self):
"""create ssender object
:return: a ssender object
"""
appid = ******
appkey = '******'
ssender = SmsSingleSender(appid, appkey)
return ssender
def send_short_message(self, phone_number):
"""Send Verification Code Short Message
:param phone_number: Short Message Receiving Number
:return: a status code
"""
ssender = self.create_ssender()
params = []
captcha = self.create_captcha()
defult_time = "2"
params.append(captcha)
params.append(defult_time)
template_id = ******
sms_sign = '******'
try:
result = ssender.send_with_param(86, phone_number, template_id, params, sign=sms_sign, extend="", ext="")
return result['result']
except HTTPError as e:
print(e)
except Exception as e:
print(e)After the template and signature are approved, instantiate Captcha() and call send_short_message() with the target phone number to deliver the verification code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
