#coding=utf-8 ''' Created on 2018年12月11日 @author: xiaojincai ''' import os import json import random import datetime from hashlib import md5 import threading import logging import base64 import requests logger = logging.getLogger(__name__) SMS_TEMPLATE_DCT = { "version":"v2.4", "sign":u"【网安云防御】", "396732":{ "template_id":"396732", "title":u"用户信息邮件变更通知", "content":u"您的注册邮箱地址在{}由{}变更为{}。如有疑问,请及时与运营人员联系!" }, "400033":{ "template_id":"400033", "title":u"用户信息手机号变更通知", "content":u"您的注册手机号码在{}由{}变更为{}。如有疑问,请及时与运营人员联系!" }, "396735":{ "template_id":"396735", "title":u"账号到期提醒通知", "content":u"您的账号{}将于{}到期,届时该账号下的网站将被停止安全防护服务,请及时与运营人员联系!" }, "396737":{ "template_id":"396737", "title":u"账号到期提醒通知(运营)", "content":u"{}账号将于{}到期,请及时处理!" }, "396739":{ "template_id":"396739", "title":u"网站迁出提醒通知(用户)", "content":u"经检测发现,您的网站{}在{}已迁出云防御系统,处于未防御状态,请予以关注!" }, "396740":{ "template_id":"396740", "title":u"网站迁出提醒通知(运营)", "content":u"经检测发现,网站{}在{}已迁出云防御系统,处于未防御状态,请及时处理!" }, "396742":{ "template_id":"396742", "title":u"防御状态变更通知(用户)", "content":u"您的网站{}在{}变更为{}状态,请予以关注!" }, "396744":{ "template_id":"396744", "title":u"防御状态变更通知(运营)", "content":u"网站{}在{}变更为{}状态,请及时处理!" }, "396746":{ "template_id":"396746", "title":u"网站删除通知", "content":u"网站{}在{}被用户{}删除,云防御系统将无法继续提供安全防护服务,请予以关注!" }, "397198":{ "template_id":"397198", "title":u"网站防护策略修改通知", "content":u"网站{}在{}被用户{}修改了{}策略,请予以关注!" }, "400981":{ "template_id":"400981", "title":u"网站到期提醒", "content":u"您的网站{}将于{}到期,届时该网站将会被自动回源,停止安全防护服务,请及时与运营人员联系!" }, "400984":{ "template_id":"400984", "title":u"网站到期提醒(运营)", "content":u"{}网站将于{}到期,请及时处理!" }, "401151":{ "template_id":"401151", "title":u"重置密码短信验证码", "content":u"您正在进行重置密码操作,验证码是{},请于{}分钟内正确输入!" }, "423122":{ "template_id":"423122", "title":u"网站可用状态变更", "content":u"您的网站{}在{}{}为{}状态,请予以关注!" } } class CloopenSMS(object): """融联短信接口 """ def __init__(self): self.sid = "8a216da8588b296f01588ecd0a0001ab" self.token = "f0d2b53782374ebc8a76fa8dd06bb4d2" self.appId = "8a216da8589ae4840158a89b5c8f0272" self.base_url = "https://app.cloopen.com:8883/2013-12-26/Accounts/%s/SMS/TemplateSMS" % self.sid def send(self,phones,template_id,content): """ @attention: 发送短信 @param content: 短信内容模板变量数组 @param phones: 电话号码集合,格式为数组 """ #构造url now = datetime.datetime.now() batch = now.strftime("%Y%m%d%H%M%S") signature = self.sid + self.token + batch sig = md5(signature).hexdigest().upper() url = self.base_url + "?sig=" + sig #构造header src = "%s:%s"%(self.sid,batch) auth = base64.encodestring(src).strip() headers = {"Accept": "application/json", "Content-Type": "application/json;charset=utf-8", "Authorization": auth} #构造body content = map(lambda x:x if isinstance(x, unicode) else str(x).decode("utf8"), content) print "send sms >>>:",phones,template_id,content body = {"to": ','.join(phones), "appId": self.appId, "templateId": template_id, "datas": content } #成功直接返回1,失败记录日志,并返回0 status_code = "" status_msg = "" response = requests.post(url, headers=headers, data=json.dumps(body)) response_code = response.status_code if response_code==200: data = response.json() if data["statusCode"]=="000000": return 1,data else: status_code = data["statusCode"] status_msg = data["statusMsg"] return 0,data #内容,模板ID,号码,http错误码,定义的错误码,操作时间,url now_time = now.strftime("%Y-%m-%d %H:%M:%S") msg = "<<(%s_%s_%s) %s>>:url=%s;\n"%(response_code,status_code,status_msg,now_time,url) sc = ",".join(content) sp = ",".join(phones) msg += "content=[%s];template_id=%s;phones=[%s]\n"%(sc,template_id,sp) return 0,u"未知错误(短信接口报错)" cloopensms = CloopenSMS() if __name__ == '__main__': #测试 phones = ['15982456282'] template_id = '396732' content = ["2018-12-11 14:00:00","496498291@qq.com","496498291@qq.com"] cloopensms.send(phones,template_id,content)