验证码发送到手机上 购买服务器进行发送短信;阿里云/ 腾讯云
from tencentcloud.common import credential from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.sms.v20190711 import sms_client, models try: cred = credential.Credential("AKIDkGlz4tb2i8G9bbPI", "nKtDmQSO5WhiU") print('101行',cred) client = sms_client.SmsClient(cred, "ap-beijing") req = models.SendSmsRequest() # 短信应用ID: 短信SdkAppid在 [短信控制台] 添加应用后生成的实际SdkAppid,示例如1400006666 req.SmsSdkAppid = "14003233333338" # 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 req.Sign = "wwwwwwww" # 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号] # 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 req.PhoneNumberSet = [‘手机号’,] # 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 req.TemplateID = "111111" # 模板参数: 若无模板参数,则设置为空 req.TemplateParamSet = [‘自己写’’,] # 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的。 # 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应。 resp = client.SendSms(req) # 输出json格式的字符串回包 print(resp.to_json_string(indent=2)) except TencentCloudSDKException as err: print(err)
python 通用
1. 安装SDK
pip install qcloudsms_py
2 基于SDK发送短信
import ssl # ssl._create_default_https_context = ssl._create_unverified_context from qcloudsms_py import SmsMultiSender, SmsSingleSender from qcloudsms_py.httpclient import HTTPError def send_sms_single(phone_num, template_id, template_param_list): """ 单条发送短信 :param phone_num: 手机号 :param template_id: 腾讯云短信模板ID :param template_param_list: 短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板 :return: """ appid = 112142311 # 自己应用ID appkey = "8cc5b87123y423423412387930004" # 自己应用Key sms_sign = "Python之路" # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称) sender = SmsSingleSender(appid, appkey) try: response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign) except HTTPError as e: response = {'result': 1000, 'errmsg': "网络异常发送失败"} return response def send_sms_multi(phone_num_list, template_id, param_list): """ 批量发送短信 :param phone_num_list:手机号列表 :param template_id:腾讯云短信模板ID :param param_list:短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板 :return: """ appid = 112142311 appkey = "8cc5b87123y423423412387930004" sms_sign = "Python之路" sender = SmsMultiSender(appid, appkey) try: response = sender.send_with_param(86, phone_num_list, template_id, param_list, sign=sms_sign) except HTTPError as e: response = {'result': 1000, 'errmsg': "网络异常发送失败"} return response if __name__ == '__main__': result1 = send_sms_single("15131255089", 548760, [666, ]) print(result1) result2 = send_sms_single( ["15131255089", "15131255089", "15131255089", ],548760, [999, ]) print(result2)