本节源码已经上传到csdn中
https://download.csdn.net/download/m0_55584761/72612124后续持续更新
这节课我们继续实现短信发送的功能。由于Django本身没有发送短信的功能,所以,我们使用第三方平台——容联云平台,完成短信功能的封装。
from ronglian_sms_sdk import SmsSDK
import json
accId = '8a216da879c0854b0179c2767c06010c'
accToken = 'af873c0294de46dd856239927df45110'
appId = '8a216da879c0854b0179c2767ce80112'
# 单例模式
class CCP(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = super(CCP, cls).__new__(cls, *args, **kwargs)
cls._instance.sdk = SmsSDK(accId, accToken, appId)
return cls._instance
def send_message(self, tid, mobile, datas):
sdk = self._instance.sdk
resp = sdk.sendMessage(tid, mobile, datas)
result = json.loads(resp)
if result["statusCode"] == "000000":
return 0
else:
return -1
if __name__ == '__main__':
c = CCP()
c.send_message(1, '15595070282', ('1234', '5'))