Most complete!!! python uses Tencent cloud service to develop SMS Verification Code Project

python uses Tencent cloud service to develop short-term Verification Code Project

Note: Recently, the company will develop a small program, and then the user logs in to this module, uses the function of SMS verification code, and then writes an article to make records.

Each new user of Tencent cloud will have 100 free text messages to use. It's enough to learn and test. This is quite good. But this free SMS has a time limit of about three months. It won't work after this time.

Each new user of Tencent cloud will have 100 free SMS to use. After authentication, 100 will be given away. There will also be a time limit

environment

Development language: Python 3
Development module: qcloudsms_py
Cloud service: Tencent cloud

Tencent Cloud Application

To use the verification code function of Tencent cloud, we need to register the account of Tencent cloud service. Tencent cloud website: Click me to jump to Tencent cloud Then we need to register an account, apply for cloud application, apply for configuring SMS signature and SMS template. Then you can use it.

1. Register / log in to Tencent cloud account

2. After successful registration, or click login to jump to the wechat login page, then log in with wechat scanning code, click OK to log in to Tencent cloud and jump to the Tencent cloud home page.

3. On Tencent cloud homepage, click product - > Enterprise Application and cloud communication - > select the SMS service in cloud communication, and you will jump to the SMS service page. Click me to jump directly to the SMS cloud service page

4. Click to receive SMS for free and jump to SMS service

5. We open the SMS service page - > select application management - > view the application list, which contains our own service applications.

6. There is only one system default application among new users. We click create application to create an application we test / use. Enter the application name and introduction.

7. After creating an application, we can enter the application to view the application information. SKD AppID and App Key are the parameters we need during development. Appid is the unique ID of the SMS application. appkey is the password used to verify the legitimacy of SMS sending. appkey needs to be kept confidential.

8. Click domestic SMS - > select signature management - > create signature. My official account is official account. Because I have a public number, I choose the public number in the signature type, and the signature is the name of my official account. Just fill in according to your needs. If you don't understand, you can see the audit standard. After creation, wait for approval.

9. Click domestic SMS - > body template management - > create body template. The template name is the message prompt of the verification code SMS, where it comes from. For example, the verification code template name of Telecom is China Telecom, and then select the template you use. You can also use a custom template, but the success rate of using Tencent's standard template is higher. The application description is generally used to verify the user and improve the authenticity of the user's mobile phone number. Create a template and wait for approval.

10. After the signature and template are created, you can wait for approval. The cloud says that the audit time is 2 hours. Generally, the audit is completed in 20 minutes. It is also normal to encounter unsuccessful audit. If it is unsuccessful, check whether there is any problem with your application. There will be a prompt of audit failure. If there is no problem, apply more times. The landlord applied for 5, 6, 7 and 8 times. O(∩_∩)O

Back end code

import ssl
import random
ssl._create_default_https_context = ssl._create_unverified_context
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

# Obtain the appid and appkey of the cloud SMS application
SDKAppID = 'Applied appid'
AppKey = 'Applied appkey'
#autograph
sms_sign = 'Created signature'
# Template ID
template_id = 'Created template ID'
# Enter your phone number
phone_number = 'Enter your phone number'

# Get random 6 digits
def captcha():
    captcha = ''
    for i in range(6):
        now_number = str(random.randint(0, 9))
        captcha += now_number
    return captcha

# Send mobile phone number verification code
def send_pcode(captcha):
    print('Get verification code',captcha)

    ssender = SmsSingleSender(SDKAppID, AppKey)
    # The parameters in the template are {1} {2}
    params = [captcha, "5"]  # When the template has no parameters, ` params = []`
    try:
        result = ssender.send_with_param(86, phone_number,
                                         template_id, params, sign=sms_sign, extend="", ext="")  # The signature parameter cannot be an empty string
        print(result)
    except HTTPError as e:
        print(e)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    send_pcode(captcha())

Verification code effect example

Where [sugar in your pocket] is the signature content, the 6-digit verification code is {1} in the template parameters, and 5 minutes is {2} in the template parameters

Tags: Python wechat

Posted on Tue, 28 Sep 2021 04:26:01 -0400 by doogles