libs->tx_sms->send_sms.py

  1. from qcloudsms_py import SmsMultiSender, SmsSingleSender
  2. from qcloudsms_py.httpclient import HTTPError
  3. import ssl
  4. ssl._create_default_https_context = ssl._create_unverified_context
  5. def get_code():
  6. import random
  7. return str(random.randint(100000, 999999))
  8. def send_message(phone_num, template_param_list, template_id='951999'):
  9. """
  10. phone_num:手机号
  11. template_id:短信模板ID
  12. template_param_list:短信模板需要的参数:例如(参数1,参数2)
  13. """
  14. appid = '1400519000' # 应用列表:SDK App
  15. appkey = 'db79dbade68a6f06d5594f7fabffeb87' # 应用列表:App Key
  16. sms_sign = '小蜗' # 腾讯云创建的签名
  17. sender = SmsSingleSender(appid, appkey)
  18. try:
  19. response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
  20. print(response)
  21. except HTTPError as e:
  22. response = {'code': 500, 'errmsg': "网络异常发送失败"}
  23. return response
  24. if __name__ == '__main__':
  25. send_message(phone_num='17615405023',template_param_list=[get_code()])

调用

  1. from libs.tx_sms import send_sms
  2. code = send_sms.get_code()
  3. phone = '17615405023'
  4. send_sms.send_message(phone_num=phone,template_param_list=[gcode,])