博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Python调用阿里云的短信接口
阅读量:5011 次
发布时间:2019-06-12

本文共 3376 字,大约阅读时间需要 11 分钟。

#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Frank import uuid import datetime import hmac import base64 import requests from urllib.parse import urlencode, quote class AliyunSMS(object):     def __init__(self):         self.format = "JSON"         self.version = "2017-05-25"         self.key = '你的key'         self.secret = '你的secret'         self.signature = ""         self.signature_method = "HMAC-SHA1"         self.signature_version = "1.0"         self.signature_nonce = str(uuid.uuid4())         self.timestamp = datetime.datetime.utcnow().isoformat("T")         self.region_id = 'cn-hangzhou'         self.gateway = "http://dysmsapi.aliyuncs.com"         self.action = "SendSms"         self.sign = ""         self.template = ""         self.params = {}         self.phones = ""     def send_single(self, phone, sign, template, params):         self.action = "SendSms"         self.phones = phone         self.sign = sign         self.params = params         self.template = template         query_string = self.build_query_string()         resp = requests.get(self.gateway + "?" + query_string).json()         model = resp.get("Model")         if model is not None:             return True         # print(model)         print("send sms to %s , reason: %s" % (self.phones, resp.get("Message")))         print(resp)         return False     def build_query_string(self):         query = []         query.append(("Format", self.format))         query.append(("Version", self.version))         query.append(("AccessKeyId", self.key))         query.append(("SignatureMethod", self.signature_method))         query.append(("SignatureVersion", self.signature_version))         query.append(("SignatureNonce", self.signature_nonce))         query.append(("Timestamp", self.timestamp))         query.append(("RegionId", self.region_id))         query.append(("Action", self.action))         query.append(("SignName", self.sign))         query.append(("TemplateCode", self.template))         query.append(("PhoneNumbers", self.phones))         params = "{"         for param in self.params:             params += "\"" + param + "\"" + ":" + "\"" + str(self.params[param]) + "\"" + ","         params = params[:-1] + "}"         # print(params)         query.append(("TemplateParam", params))         query = sorted(query, key=lambda key: key[0])         query_string = ""         i = 0         for item in query:             query_string += quote(item[0], safe="~") + "=" + quote(item[1], safe="~") + "&"             i += 1             print(query_string)         # print(i)         query_string = query_string[:-1]         # print(query_string)         tosign = "GET&%2F&" + quote(query_string, safe="~")         secret = self.secret + "&"         hmb = hmac.new(secret.encode("utf-8"), tosign.encode("utf-8"), "sha1").digest()         self.signature = quote(base64.standard_b64encode(hmb).decode("ascii"), safe="~")         query_string += "&" + "Signature=" + self.signature         print('query_string',query_string)         return query_string sms = AliyunSMS() import random l = [] for i in range(6):     s = str(random.randint(0, 10))     l.append(s) ss = ''.join(l) # print('ss', ss) params = {"code": ss}#这就是随机生成的6位数 sms.send_single(phone="这就是你想给谁发电话号码", sign="你注册时的sign", template='你注册的模板名称', params=params)

转载于:https://www.cnblogs.com/1a2a/p/8778242.html

你可能感兴趣的文章
22-reverseString-Leetcode
查看>>
Centos 开机自动联网
查看>>
cocos2dx使用lua和protobuf
查看>>
使用Spring配合Junit进行单元测试的总结
查看>>
HDOJ 5630 Rikka with Chess
查看>>
netcore2.1 在后台运行一个任务
查看>>
PostgreSQL pg_hba.conf 文件简析
查看>>
android o logcat read: unexpected EOF!
查看>>
[Scrum]2010/12/28 —— 第一天!
查看>>
ASP.NET MVC模式 温习(一)排除MVC模式误区
查看>>
Mysql的read_only 只读属性说明 (运维笔记)
查看>>
DOCKER 从入门到放弃(五)
查看>>
Python 多线程学习
查看>>
appcan官方ajax
查看>>
获取NVIDIA显卡的温度
查看>>
Dijkstra算法
查看>>
Deep Learning 9: Performance
查看>>
面试题61 把二叉树打印成多行
查看>>
C#例子 易懂故事 接口 委托 事件 异步通知 好玩.
查看>>
sqlce wp
查看>>