Tencent launched Cloud SMS
"" " 1, the official website of the real-name registration account: HTTPS: //cloud.tencent.com 2, select Create a text messaging service application 3, signed application and SMS templates - through public micro-channel number to apply ." ""
Cloud messages Tencent second package
libs ├── txsms │ ├── __init__.py │ ├── settings.py └ └── sms.py
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) # 发送验证码 from utils.logging import logger def send_sms(mobile, code, exp): try: # 发送短信 response = sender.send_with_param(MOBILE_PREFIX, mobile, TEMPLATE_ID, (code, exp), sign=SMS_SIGN, extend="", ext="") # 成功 if response and response['result'] == 0: return True # 失败 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))
Case
# Send code interfaces from libs Import txsms class SMSAPIView (APIView): DEF POST (Self, Request, * args, ** kwargs): # verify the legitimacy of the phone number again Mobile request.data.get = ( ' Mobile ' ) # verify the phone number exist and legitimate iF not Mobile or not re.match (R & lt ' ^. 1 [3-9] \ {D} $. 9 ' , Mobile): return APIResponse (. 1, ' the phone number is not valid ' ) # generated codes code =txsms.get_code () # transmit codes Result = txsms.send_sms (Mobile, code, SMS_EXP // 60 ) IF Not Result: return (. 1, APIResponse ' verification code failed ' ) # transmit codes to redis successfully saved in easy management cache.set (SMS_CACHE_KEY% { ' Mobile ' : Mobile}, code, SMS_EXP) return APIResponse (0, ' verification code transmission success ' )