您的位置:首页 > 编程语言 > Python开发

秒嘀(miaodi)短信群发平台demo(python版)

2018-02-08 14:31 471 查看
#!/usr/bin/python
#-*-coding:utf-8-*-

#秒嘀短信API实现
# Refer to: http://www.miaodiyun.com/doc/guide.html 
import httplib,urllib,hashlib,datetime,time,json,ssl;  #加载模块

#发送行业短信
def sendIndustrySms(tos, smsContent):

#定义账号和密码,开户之后可以从用户中心得到这两个值
accountSid = 'xxx';
acctKey = 'xxx';

#定义地址,端口等
serverHost = "api.miaodiyun.com";
serverPort = 443;
industryUrl = "/20150822/industrySMS/sendSMS";

#格式化时间戳,并计算签名
timeStamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S');
rawsig = accountSid + acctKey + timeStamp;
m = hashlib.md5();
m.update(rawsig);
sig = m.hexdigest();

#定义需要进行发送的数据表单
params = urllib.urlencode({'accountSid':accountSid,
'smsContent':smsContent,
'to':tos,
'timestamp':timeStamp,
'sig':sig});

#定义header
headers = {"Content-Type":"application/x-www-form-urlencoded", "Accept":"application/json"};

#与构建https连接
conn = httplib.HTTPSConnection(serverHost, serverPort);

#Post数据
conn.request(method = "POST", url = industryUrl, body = params, headers = headers);

#返回处理后的数据
response = conn.getresponse();
#读取返回数据
jsondata = response.read().decode('utf-8');

#打印完整的返回数据
print jsondata;

#解析json,获取特定的几个字段
jsonObj = json.loads(jsondata);
respCode = jsonObj['respCode'];
print "错误码:" , respCode;
respDesc = jsonObj['respDesc'];
print "错误描述:" , respDesc;

#关闭连接
conn.close();

#tos可以是一个或者多个号码,若是多个号码,以英文逗号分开
tos = '18665152605';
#短信内容
smsContent = '【秒嘀科技】您的秒嘀科技验证码是888888,5分钟有效。';
#提交短信
sendIndustrySms(tos, smsContent);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: