短信接口的使用(腾讯云)
#
""" 1、官网注册实名账号:https://cloud.tencent.com 2、选取短信服务创建短信应用 3、申请签名与短信模板 - 通过微信公众号申请
4、帮助文档:https://cloud.tencent.com/document/product/382/43196 """
腾讯云短信的二次封装:#
#
# 短信应用 SDK AppID - SDK AppID 以1400开头 APP_ID = ... # 短信应用 SDK AppKey APP_KEY = "..." # 短信模板ID,需要在短信控制台中申请 TEMPLATE_ID = ... # 签名 - 是`签名内容`,而不是`签名ID` SMS_SIGN= "..." # 电话前缀 MOBILE_PREFIX = 86
#
# 通过MacOS ssl安全认证 import ssl ssl._create_default_https_context = ssl._create_unverified_context # 获取验证码的功能 import random def get_code(): code = '' for i in range(4): code += str(random.randint(0, 9)) return code # 短信发送者 from qcloudsms_py import SmsSingleSender from .settings import * sender = SmsSingleSender(APP_ID, APP_KEY) # 发送验证码 from utils.logging import logger def send_sms(mobile, code, exp): try: # 发送短信 response = sender.send_with_param(MOBILE_PREFIX, mobile, TEMPLATE_ID, (code, exp), sign=SMS_SIGN, extend="", ext="") # 成功 if response and response['result'] == 0: return True # 失败 logger.warning('%s - %s' % ('短信发送失败', response['result'])) except Exception as e: # 异常 logger.warning('%s - %s' % ('短信发送失败', e)) return False
#
# 包对外提供的功能方法 from .sms import get_code, send_sms
#
from libs import txsms code = txsms.get_code() print(code) print(txsms.send_sms('电话', code, 5))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)