Python全系列 教程
3567个小节阅读:5930k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
短信验证,首先得选择一个短信发送服务器上,本文档使用阿里云实现短信发送功能
注册账号
新账号赠送100条,可以不用充值,即可进行测试
接入 短信
进行 个人实名认证
编写代码执行
安装依赖模块
xxxxxxxxxx
pip install alibabacloud_dysmsapi20170525==2.0.23
xxxxxxxxxx
# -*- coding: utf-8 -*-
import sys
from typing import List
from alibabacloud_dysmsapi20170525.client import Client as Dysmsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dysmsapi20170525 import models as dysmsapi_20170525_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_console.client import Client as ConsoleClient
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> Dysmsapi20170525Client:
"""
使用AK&SK初始化账号Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret
)
# 访问的域名
config.endpoint = f'dysmsapi.aliyuncs.com'
return Dysmsapi20170525Client(config)
@staticmethod
def create_client_with_sts(
access_key_id: str,
access_key_secret: str,
security_token: str,
) -> Dysmsapi20170525Client:
"""
使用STS鉴权方式初始化账号Client,推荐此方式。本示例默认使用AK&SK方式。
@param access_key_id:
@param access_key_secret:
@param security_token:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret,
# 必填,您的 Security Token,
security_token=security_token,
# 必填,表明使用 STS 方式,
type='sts'
)
# 访问的域名
config.endpoint = f'dysmsapi.aliyuncs.com'
return Dysmsapi20170525Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
client = Sample.create_client('ACCESS_KEY_ID', 'ACCESS_KEY_SECRET')
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
sign_name='阿里云短信测试',
template_code='SMS_154950909',
phone_numbers='17703181473',
template_param='{"code":"1234"}'
)
runtime = util_models.RuntimeOptions()
resp = client.send_sms_with_options(send_sms_request, runtime)
ConsoleClient.log(UtilClient.to_jsonstring(resp))
@staticmethod
async def main_async(
args: List[str],
) -> None:
# 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html
client = Sample.create_client('ACCESS_KEY_ID', 'ACCESS_KEY_SECRET')
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
sign_name='阿里云短信测试',
template_code='SMS_154950909',
phone_numbers='186001000001',
template_param='{"code":"1234"}'
)
runtime = util_models.RuntimeOptions()
resp = await client.send_sms_with_options_async(send_sms_request, runtime)
ConsoleClient.log(UtilClient.to_jsonstring(resp))
if __name__ == '__main__':
Sample.main()
实时学习反馈
1.使用阿里云短信实现短信发送功能,实名信息审核通过后,通过ACCESS_KEY_ID和______参数确定用户信息
A 域名
B 短信模板
C ACCESS_KEY_SECRET
D 验证码
答案
1=>C