发送短信(验证码)

你还在愁不会利用py发送短信吗?快来瞧瞧我是这么利用py发送的吧!(仅供参考学习,如有用在别的用途与作者无关)

from tencentcloud.common import credential

from tencentcloud.common.profile.client_profile import ClientProfile

from tencentcloud.common.profile.http_profile import HttpProfile

from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

from tencentcloud.sms.v20190711 import sms_client, models


def sms(phone):

    """

    :param phone: 手机号

    :return: 接口返回信息

    """

    try:

        cred = credential.Credential("id", "key") # API接口的 id key

        httpProfile = HttpProfile()

        httpProfile.endpoint = "sms.tencentcloudapi.com"


        clientProfile = ClientProfile()

        clientProfile.httpProfile = httpProfile

        client = sms_client.SmsClient(cred, "", clientProfile)


        req = models.SendSmsRequest()

        params = {

            "PhoneNumberSet": ['+86' + phone],

            "TemplateParamSet": [] # 模板里的参数,

            "TemplateID": "" # 模板id,

            "SmsSdkAppid": "" # 你的sdk appID,

            "Sign": "" # 备案名字(签名)

        }

        req.from_json_string(json.dumps(params))


        resp = client.SendSms(req)

        print('sms:短信验证码发送成功!', resp.to_json_string())

        return resp.to_json_string()


    except TencentCloudSDKException as err:

        print('sms: 接口异常。', err)

        return err

  • 发表于 2021-02-23 21:34
  • 阅读 ( 378 )
  • 分类:默认分类

你可能感兴趣的文章

相关问题

9 条评论

请先 登录 后评论
小斌
小斌

1 篇文章

作家榜 »

  1. 阿九 20 文章
  2. q5320 14 文章
  3. 不期而遇 8 文章
  4. admin 7 文章
  5. 此心安處是吾鄉 4 文章
  6. 小白 4 文章
  7. Mr.Pang 3 文章
  8. yixinBC 3 文章