ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

python --阿里短信服务

python --阿里短信服务 安装:pip install aliyun-python-sdk-core2.13.30代码:importrandomfromaliyunsdkcore.clientimportAcsClientfromaliyunsdkcore.requestimportCommonRequest# from consulting_service.settings import AccessKeyId, AccessKey_Secretimportrefromfunctoolsimportpartial INTEGER_PATTERNre.compile(r^[1-9]\d*$)USERNAME_PATTERNre.compile(r^[a-zA-Z0-9_\-\.]{6,20}$)TEL_PATTERNre.compile(r^1[3-9]\d{9}$)EMAIL_PATTERNre.compile(r^([a-zA-Z0-9_\-\.])r((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|r(([a-zA-Z0-9\-]\.)))([a-zA-Z]{2,4}|r[0-9]{1,3})(\]?)$,re.VERBOSE)URL_PATTERNre.compile(r^(?:http|ftp)s?://r(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|rlocalhost|r\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})r(?::\d)?r(?:/?|[/?]\S)$,re.IGNORECASE)defcheck_with_pattern(pattern,value,*,hintFalse):使用正则表达式检查输入的值ifhint:returnifpattern.match(value)elsef{value}is invalidelse:returnpattern.match(value)check_integerpartial(check_with_pattern,INTEGER_PATTERN)check_usernamepartial(check_with_pattern,USERNAME_PATTERN)check_telpartial(check_with_pattern,TEL_PATTERN)check_emailpartial(check_with_pattern,EMAIL_PATTERN)check_urlpartial(check_with_pattern,URL_PATTERN)defsend_sms(*,tel,times):发送短息clientAcsClient(AccessKeyId,AccessKey_Secret,default)requestCommonRequest()request.set_accept_format(json)request.set_domain(dysmsapi.aliyuncs.com)request.set_method(POST)request.set_protocol_type(https)# https | httprequest.set_version(2017-05-25)request.set_action_name(SendSms)request.add_query_param(RegionId,default)request.add_query_param(PhoneNumbers,tel)# 发给谁request.add_query_param(SignName,润心教育)# 签名request.add_query_param(TemplateCode,SMS_220620035)# 模板编号request.add_query_param(TemplateParam,{time:times})# 模板里面的变量 要一致 {name: name, time: times}responseclient.do_action_with_exception(request)print(发送短信:times:{}.format(times))resjson.loads(str(response,encodingutf-8))returnresdefgen_mobile_code(length6):生成指定长度的手机验证码return.join(random.choices(0123456789,klength))if__name____main__:print(send_sms(times2021-07-12 18:00:00,tel170495885**))异步版本pip install alibabacloud_dysmsapi201705252.0.22-- coding: utf-8 --This file is auto-generated, don’t edit it. Thanks.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_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: 使用AKSK初始化账号Client param access_key_id: param access_key_secret: return: Client throws Exception config open_api_models.Config( # 您的 AccessKey ID, access_key_idaccess_key_id, # 您的 AccessKey Secret, access_key_secretaccess_key_secret ) # 访问的域名 config.endpoint fdysmsapi.aliyuncs.com return Dysmsapi20170525Client(config) staticmethod def main( args: List[str], ) - None: client Sample.create_client(accessKeyId, accessKeySecret) send_sms_request dysmsapi_20170525_models.SendSmsRequest( sign_name阿里云短信测试, template_codeSMS_154950909, phone_numbers18691962070, template_param{code:1234} ) runtime util_models.RuntimeOptions() try: # 复制代码运行请自行打印 API 的返回值 client.send_sms_with_options(send_sms_request, runtime) except Exception as error: # 如有需要请打印 error UtilClient.assert_as_string(error.message) staticmethod async def main_async( args: List[str], ) - None: client Sample.create_client(accessKeyId, accessKeySecret) send_sms_request dysmsapi_20170525_models.SendSmsRequest( sign_name阿里云短信测试, template_codeSMS_154950909, phone_numbers18691962070, template_param{code:1234} ) runtime util_models.RuntimeOptions() try: # 复制代码运行请自行打印 API 的返回值 await client.send_sms_with_options_async(send_sms_request, runtime) except Exception as error: # 如有需要请打印 error UtilClient.assert_as_string(error.message) if __name__ __main__: Sample.main(sys.argv[1:])短信认证(个人资质 无法自定义签名和模板)# -*- coding: utf-8 -*-fromloguruimportloggerfromalibabacloud_dypnsapi20170525.clientimportClientasDypnsapi20170525Clientfromalibabacloud_tea_openapiimportmodelsasopen_api_modelsfromalibabacloud_dypnsapi20170525importmodelsasdypnsapi_20170525_modelsfromalibabacloud_tea_utilimportmodelsasutil_models# pip install alibabacloud_dypnsapi201705252.0.0classSendMsg(object):阿里短信认证def__init__(self,ak,sk):configopen_api_models.Config(access_key_idak,access_key_secretsk)config.endpointdypnsapi.aliyuncs.comself.clientDypnsapi20170525Client(config)defsend(self,tel,code):发送try:send_sms_verify_code_requestdypnsapi_20170525_models.SendSmsVerifyCodeRequest(phone_numbertel,sign_name恒创联众,template_code100001,template_paramstr({code:code,min:10}))runtimeutil_models.RuntimeOptions()respself.client.send_sms_verify_code_with_options(send_sms_verify_code_request,runtime)logger.warning(f手机号:【{tel}】;验证码:【{code}】)logger.debug(f短信发送结果:【{resp}】)exceptExceptionase:logger.error(f{e})if__name____main__:akxxxxxxxskxxxxaSendMsg(ak,sk)a.send(xxxxxxx,888888)
返回列表