SMS Authentication

A message authentication

https://cloud.tencent.com/document/product/382/11672

1. Download module

pip install qcloudsms_py

2. noodles edition

# SMS app SDK AppID 
AppID = 1400279725   # SDK AppID to 1400 begins with 
# SMS app SDK AppKey 
AppKey = " f1a377c327812cdd242ccb66a2fe895b " 
# need to send an SMS mobile phone number 
phone_numbers = [ " 18,539,419,683 " ]
 # SMS templates ID, you need to apply for the SMS console 
template_id 457 273 =   # NOTE: here's an example of template ID`7839` only real need to apply the template ID in the message console 
# signature 
sms_sign = " sages "   # NOTE: signature parameters used are `` signature content, rather than `signature ID `. Here's signature "Tencent cloud" is just an example, the real need to apply for signature in the message console 

# random verification code 
Import Random
 DEF GET_CODE ():
    code = '' 
    for I in Range (. 4 ): 
        code + = STR (the random.randint (0,9 ))
     return code 

from qcloudsms_py Import SmsSingleSender
 from utils.logging Import Logger 

IF  the __name__ == ' __main__ ' : 

    ssender = SmsSingleSender ( AppID, AppKey)
     # verification code, and expiration time message template placeholder 
    code = GET_CODE ()
     Print (code) 
    the params = [code,. 5]   #When no template parameters, the params = `[]`
    the try : 
      Result = ssender.send_with_param (86 , phone_numbers [0], 
          template_id, the params, Sign = sms_sign, Extend = "" , EXT = "" )
       IF Result and Result [ ' Result ' ] == 0:
           Print ( ' transmission success ' )
     the except Exception AS E:
       Print (E) 
      logger.warning (E) 
      Print ( " failed to send SMS ' )

3. Package Version

libs/txsms/settings.py
# SMS app SDK AppID - SDK AppID beginning with 1400 
APP_ID = ...
 # messaging applications AppKey SDK 
APP_KEY = " ... " 
# SMS templates ID, need to apply for the SMS console 
TEMPLATE_ID = ...
 # signature - is `signature `content, rather than` signature ID` 
SMS_SIGN = " ... " 
# telephone prefix 
MOBILE_PREFIX = 86
libs/txsms/sms.py
# By MacOS ssl security authentication 
Import SSL 
ssl._create_default_https_context = ssl._create_unverified_context 

# function of acquiring a verification code 
Import Random
 DEF GET_CODE (): 
    code = '' 
    for I in Range (. 4 ): 
        code + = STR (the random.randint (0 ,. 9 ))
     return code 

# message senders 
from qcloudsms_py Import SmsSingleSender
 from .settings Import * 
sENDER = SmsSingleSender (APP_ID, APP_KEY) 

#Send verification code 
from utils.logging Import Logger 
# Timeout number verification code
DEF send_sms (Mobile, code, exp): the try : # Send SMS templates id number prefix number code expiration date signed content response = sender.send_with_param (MOBILE_PREFIX, mobile , TEMPLATE_ID, (code, exp), Sign = SMS_SIGN, Extend = "" , EXT = "" ) # successful IF Response and Response [ ' Result ' ] == 0: return True # failed logger.warning (' % S -% S ' % ( ' Message not sent ' , Response [ ' Result ' ])) the except Exception AS E: # exception logger.warning ( ' % S -% S ' % ( ' Message not sent ' , E )) return False
libs/txsms/__init__.py
# Function method package provided externally 
from .sms Import GET_CODE, send_sms
test
from libs import txsms
code = txsms.get_code()
print(code)
print(txsms.send_sms('电话', code, 5))

 

Guess you like

Origin www.cnblogs.com/tfzz/p/11764047.html