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

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

#!/usr/bin/env python #-*- coding:utf-8 -*- #Author:lzd 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 = 'xxx'         self.secret = 'xxx'         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)         return query_string sms=AliyunSMS() params = {"code":"test","product":"test2"} sms.send_single(phone="13900000000", sign="猎宝网", template='SMS_33435156', params=params)

转载地址:http://wnrha.baihongyu.com/

你可能感兴趣的文章
Win10 安装msi 提示2502、2503的错误代码 -- 命令提示符(管理员) -- msiexec /package...
查看>>
组策略脚本的趣味应用
查看>>
corosync+pacemaker高可用集群
查看>>
景安河南机房电力事故,部分主机和云服务器停机
查看>>
1024x600 7" LVDS LCD with Capacitive Touch for pcD
查看>>
解决ubuntu系统安装完后启动项里没有ubuntu的问题
查看>>
Objective-C中new与alloc/init的区别
查看>>
手动编译Mysql5.6.10 手动编译php 支持fastcgi
查看>>
MySQL主主
查看>>
linux的权限管理以及特殊权限SUID,SGID,Sticky
查看>>
大数据测试之初识Hadoop2
查看>>
linux安装nginx
查看>>
ant 之传参数
查看>>
翻转单词顺序与左旋转字符串
查看>>
在Zf2中实现Controller按照URL自动注册
查看>>
批处理的变量引用
查看>>
oracle ORACLE_SID使用上的意义
查看>>
RHEL5下安装Xen
查看>>
2011百度之星初赛B圆环
查看>>
canvas绘制时钟
查看>>