django中基于python3.6使用容联发送短信
一. Django基于python3.6使用容联发送短信流程
容联官方的python支持2.7版本,当我们python解释器采用3版本时,需要修改容联接口中的一些参数及方法。
首先去容联官网注册账号,申请测试账号无需实名认证,而且开始账号自带8元钱,一条短信大概2分。
1.1 调用接口前的准备
首先注册登录之后再控制台中添加测试号码:
点击导航栏的开发文档:
选择短信开发手册:
之后点击Demo示例:
下载基于python的Demo:
下载完成后解压,将其中的两个文件复制:
然后在python项目文件夹下新建文件夹,比如libs,里面再新建一个文件夹,将这两个文件黏贴在其中:
然后在yuntongxun文件下新建sms.py文件,同时将接口调用示例中年的代码复制黏贴至其中:
将sms.py中的报错修改一下:
以上几个参数在控制台中可以看到:
同时可以修改一下sms.py文件中调用接口的函数,修改一下返回的信息,方便后续加入自己的逻辑,这里我将其函数名也修改了:
def send_msg(to, datas, tempId):
# 初始化REST SDK
rest = REST(serverIP, serverPort, softVersion)
rest.setAccount(accountSid, accountToken)
rest.setAppId(appId) result = rest.sendTemplateSMS(to, datas, tempId)
if result.get('statusCode') == '':
print('发送成功')
return True
else:
print('发送失败')
return False
result中的信息可以在开发文档中查看:
1.2 使用容联接口
打开CCPRestSDK.py发现报错:
将其中的模块修改如下,修改三个地方:
# 修改
from hashlib import md5
import base64
import datetime
# 修改
from urllib import request as urllib2
import json
# 修改
from .xmltojson import xmltojson
from xml.dom import minidom
然后里面还有很多报错如下:
修改成以下格式即可(将Exception后的逗号去掉改为as即可),将所有该报错修改,共13个:
然后将第定位至235行:
首先修改生成sig中的内容:
#生成sig
signature = self.AccountSid + self.AccountToken + self.Batch;
md5_obj = md5()
md5_obj.md5.new(signature)
sig = md5_obj.hexdigest().upper()
再修改生成auth中的内容:
# 仅仅修改其中的auth
auth = base64.encodebytes(src.encode()).decode().strip()
再修改269行的代码:
修改如下:
req.data = body.encode()
然后775行的代码也需要加个类型转换:
使用Int将其转为整型:
if(int(self.ServerPort)<=0):
print('');
print('端口错误(小于等于0)');
接着在yuntongxun文件夹下的__init__.py中加入以下语句:
from .sms import send_msg
然后新建一个测试文件测试,需要搭建django测试环境,代码如下:
import os # 以下三行配置django测试环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.dev')
import django
django.setup() from luffyapi.libs.yuntongxun import send_msg # send_msg('接收方手机号',(验证码,过期时间分钟), 模块编号)
# 模板编号测试环境中只能为1,接收方手机号是我们控制台测试号码中配置的
# 过期时间客户端收到的是以分钟为单位的,我们通过该值配置redis等非关系型数据库中该验证码的过期时间
result = send_msg('', ('python', 5), 1)
print(result)
# -*- coding: UTF-8 -*-
# Copyright (c) 2014 The CCP project authors. All Rights Reserved.
#
# Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
# that can be found in the LICENSE file in the root of the web site.
#
# http://www.yuntongxun.com
#
# An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree. from hashlib import md5
import base64
import datetime
from urllib import request as urllib2
import json
from .xmltojson import xmltojson class REST:
AccountSid = ''
AccountToken = ''
AppId = ''
SubAccountSid = ''
SubAccountToken = ''
ServerIP = ''
ServerPort = ''
SoftVersion = ''
Iflog = False # 是否打印日志
Batch = '' # 时间戳
BodyType = 'xml' # 包体格式,可填值:json 、xml # 初始化
# @param serverIP 必选参数 服务器地址
# @param serverPort 必选参数 服务器端口
# @param softVersion 必选参数 REST版本号
def __init__(self, ServerIP, ServerPort, SoftVersion): self.ServerIP = ServerIP
self.ServerPort = ServerPort
self.SoftVersion = SoftVersion # 设置主帐号
# @param AccountSid 必选参数 主帐号
# @param AccountToken 必选参数 主帐号Token def setAccount(self, AccountSid, AccountToken):
self.AccountSid = AccountSid
self.AccountToken = AccountToken # 设置子帐号
#
# @param SubAccountSid 必选参数 子帐号
# @param SubAccountToken 必选参数 子帐号Token def setSubAccount(self, SubAccountSid, SubAccountToken):
self.SubAccountSid = SubAccountSid
self.SubAccountToken = SubAccountToken # 设置应用ID
#
# @param AppId 必选参数 应用ID def setAppId(self, AppId):
self.AppId = AppId def log(self, url, body, data):
print('这是请求的URL:')
print(url)
print('这是请求包体:')
print(body)
print('这是响应包体:')
print(data)
print('********************************') # 创建子账号
# @param friendlyName 必选参数 子帐号名称
def CreateSubAccount(self, friendlyName): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SubAccounts?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth)
# xml格式
body = '''<?xml version="1.0" encoding="utf-8"?><SubAccount><appId>%s</appId>\
<friendlyName>%s</friendlyName>\
</SubAccount>\
''' % (self.AppId, friendlyName) if self.BodyType == 'json':
# json格式
body = '''{"friendlyName": "%s", "appId": "%s"}''' % (friendlyName, self.AppId)
data = ''
req.data = body.encode()
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 获取子帐号
# @param startNo 可选参数 开始的序号,默认从0开始
# @param offset 可选参数 一次查询的最大条数,最小是1条,最大是100条
def getSubAccounts(self, startNo, offset): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/GetSubAccounts?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
# auth = base64.encodestring(src).strip()
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth)
# xml格式
body = '''<?xml version="1.0" encoding="utf-8"?><SubAccount><appId>%s</appId>\
<startNo>%s</startNo><offset>%s</offset>\
</SubAccount>\
''' % (self.AppId, startNo, offset) if self.BodyType == 'json':
# json格式
body = '''{"appId": "%s", "startNo": "%s", "offset": "%s"}''' % (self.AppId, startNo, offset)
data = ''
req.data = body.encode()
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 子帐号信息查询
# @param friendlyName 必选参数 子帐号名称 def querySubAccount(self, friendlyName): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/QuerySubAccountByName?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
# auth = base64.encodestring(src).strip()
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req) req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><SubAccount><appId>%s</appId>\
<friendlyName>%s</friendlyName>\
</SubAccount>\
''' % (self.AppId, friendlyName)
if self.BodyType == 'json':
body = '''{"friendlyName": "%s", "appId": "%s"}''' % (friendlyName, self.AppId)
data = ''
req.data = body.encode()
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 发送模板短信
# @param to 必选参数 短信接收彿手机号码集合,用英文逗号分开
# @param datas 可选参数 内容数据
# @param tempId 必选参数 模板Id
def sendTemplateSMS(self, to, datas, tempId): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SMS/TemplateSMS?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
# auth = base64.encodestring(src).strip()
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth)
# 创建包体
b = ''
for a in datas:
b += '<data>%s</data>' % (a) body = '<?xml version="1.0" encoding="utf-8"?><SubAccount><datas>' + b + '</datas><to>%s</to><templateId>%s</templateId><appId>%s</appId>\
</SubAccount>\
' % (to, tempId, self.AppId)
if self.BodyType == 'json':
# if this model is Json ..then do next code
b = '['
for a in datas:
b += '"%s",' % (a)
b += ']'
body = '''{"to": "%s", "datas": %s, "templateId": "%s", "appId": "%s"}''' % (to, b, tempId, self.AppId)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 外呼通知
# @param to 必选参数 被叫号码
# @param mediaName 可选参数 语音文件名称,格式 wav。与mediaTxt不能同时为空。当不为空时mediaTxt属性失效。
# @param mediaTxt 可选参数 文本内容
# @param displayNum 可选参数 显示的主叫号码
# @param playTimes 可选参数 循环播放次数,1-3次,默认播放1次。
# @param respUrl 可选参数 外呼通知状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知。
# @param userData 可选参数 用户私有数据
# @param maxCallTime 可选参数 最大通话时长
# @param speed 可选参数 发音速度
# @param volume 可选参数 音量
# @param pitch 可选参数 音调
# @param bgsound 可选参数 背景音编号 def landingCall(self, to, mediaName, mediaTxt, displayNum, playTimes, respUrl, userData, maxCallTime, speed, volume,
pitch, bgsound): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/LandingCalls?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
# auth = base64.encodestring(src).strip()
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><LandingCall>\
<to>%s</to><mediaName>%s</mediaName><mediaTxt>%s</mediaTxt><appId>%s</appId><displayNum>%s</displayNum>\
<playTimes>%s</playTimes><respUrl>%s</respUrl><userData>%s</userData><maxCallTime>%s</maxCallTime><speed>%s</speed>
<volume>%s</volume><pitch>%s</pitch><bgsound>%s</bgsound></LandingCall>\
''' % (
to, mediaName, mediaTxt, self.AppId, displayNum, playTimes, respUrl, userData, maxCallTime, speed, volume,
pitch, bgsound)
if self.BodyType == 'json':
body = '''{"to": "%s", "mediaName": "%s","mediaTxt": "%s","appId": "%s","displayNum": "%s","playTimes": "%s","respUrl": "%s","userData": "%s","maxCallTime": "%s","speed": "%s","volume": "%s","pitch": "%s","bgsound": "%s"}''' % (
to, mediaName, mediaTxt, self.AppId, displayNum, playTimes, respUrl, userData, maxCallTime, speed,
volume,
pitch, bgsound)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 语音验证码
# @param verifyCode 必选参数 验证码内容,为数字和英文字母,不区分大小写,长度4-8位
# @param playTimes 可选参数 播放次数,1-3次
# @param to 必选参数 接收号码
# @param displayNum 可选参数 显示的主叫号码
# @param respUrl 可选参数 语音验证码状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知
# @param lang 可选参数 语言类型
# @param userData 可选参数 第三方私有数据 def voiceVerify(self, verifyCode, playTimes, to, displayNum, respUrl, lang, userData): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/VoiceVerify?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
# auth = base64.encodestring(src).strip()
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req) req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><VoiceVerify>\
<appId>%s</appId><verifyCode>%s</verifyCode><playTimes>%s</playTimes><to>%s</to><respUrl>%s</respUrl>\
<displayNum>%s</displayNum><lang>%s</lang><userData>%s</userData></VoiceVerify>\
''' % (self.AppId, verifyCode, playTimes, to, respUrl, displayNum, lang, userData)
if self.BodyType == 'json':
# if this model is Json ..then do next code
body = '''{"appId": "%s", "verifyCode": "%s","playTimes": "%s","to": "%s","respUrl": "%s","displayNum": "%s","lang": "%s","userData": "%s"}''' % (
self.AppId, verifyCode, playTimes, to, respUrl, displayNum, lang, userData)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # IVR外呼
# @param number 必选参数 待呼叫号码,为Dial节点的属性
# @param userdata 可选参数 用户数据,在<startservice>通知中返回,只允许填写数字字符,为Dial节点的属性
# @param record 可选参数 是否录音,可填项为true和false,默认值为false不录音,为Dial节点的属性 def ivrDial(self, number, userdata, record): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch;
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/ivr/dial?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
req.add_header("Accept", "application/xml")
req.add_header("Content-Type", "application/xml;charset=utf-8")
req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?>
<Request>
<Appid>%s</Appid>
<Dial number="%s" userdata="%s" record="%s"></Dial>
</Request>
''' % (self.AppId, number, userdata, record)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close()
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 话单下载
# @param date 必选参数 day 代表前一天的数据(从00:00 – 23:59),目前只支持按天查询
# @param keywords 可选参数 客户的查询条件,由客户自行定义并提供给云通讯平台。默认不填忽略此参数
def billRecords(self, date, keywords): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/BillRecords?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><BillRecords>\
<appId>%s</appId><date>%s</date><keywords>%s</keywords>\
</BillRecords>\
''' % (self.AppId, date, keywords)
if self.BodyType == 'json':
# if this model is Json ..then do next code
body = '''{"appId": "%s", "date": "%s","keywords": "%s"}''' % (self.AppId, date, keywords)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read() res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 主帐号信息查询 def queryAccountInfo(self): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/AccountInfo?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
body = ''
req.add_header("Authorization", auth)
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 短信模板查询
# @param templateId 必选参数 模板Id,不带此参数查询全部可用模板 def QuerySMSTemplate(self, templateId): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SMS/QuerySMSTemplate?sig=" + sig
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req) req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><Request>\
<appId>%s</appId><templateId>%s</templateId></Request>
''' % (self.AppId, templateId)
if self.BodyType == 'json':
# if this model is Json ..then do next code
body = '''{"appId": "%s", "templateId": "%s"}''' % (self.AppId, templateId)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main2(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 呼叫结果查询
# @param callsid 必选参数 呼叫ID def CallResult(self, callSid): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/CallResult?sig=" + sig + "&callsid=" + callSid
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
body = ''
req.add_header("Authorization", auth)
data = ''
try:
res = urllib2.urlopen(req)
data = res.read()
res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 呼叫状态查询
# @param callid 必选参数 一个由32个字符组成的电话唯一标识符
# @param action 可选参数 查询结果通知的回调url地址
def QueryCallState(self, callid, action): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/ivr/call?sig=" + sig + "&callid=" + callid
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
self.setHttpHeader(req)
req.add_header("Authorization", auth) # 创建包体
body = '''<?xml version="1.0" encoding="utf-8"?><Request>\
<Appid>%s</Appid><QueryCallState callid="%s" action="%s"/>\
</Request>\
''' % (self.AppId, callid, action)
if self.BodyType == 'json':
# if this model is Json ..then do next code
body = '''{"Appid":"%s","QueryCallState":{"callid":"%s","action":"%s"}}''' % (self.AppId, callid, action)
req.data = body.encode()
data = ''
try:
res = urllib2.urlopen(req)
data = res.read() res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 语音文件上传
# @param filename 必选参数 文件名
# @param body 必选参数 二进制串
def MediaFileUpload(self, filename, body): self.accAuth()
nowdate = datetime.datetime.now()
self.Batch = nowdate.strftime("%Y%m%d%H%M%S")
# 生成sig
signature = self.AccountSid + self.AccountToken + self.Batch
sig = md5(signature.encode()).hexdigest().upper()
# 拼接URL
url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/MediaFileUpload?sig=" + sig + "&appid=" + self.AppId + "&filename=" + filename
# 生成auth
src = self.AccountSid + ":" + self.Batch
auth = base64.encodebytes(src.encode()).decode().strip()
req = urllib2.Request(url)
req.add_header("Authorization", auth)
if self.BodyType == 'json':
req.add_header("Accept", "application/json")
req.add_header("Content-Type", "application/octet-stream") else:
req.add_header("Accept", "application/xml")
req.add_header("Content-Type", "application/octet-stream") # 创建包体
req.data = body.encode() try:
res = urllib2.urlopen(req)
data = res.read() res.close() if self.BodyType == 'json':
# json格式
locations = json.loads(data)
else:
# xml格式
xtj = xmltojson()
locations = xtj.main(data)
if self.Iflog:
self.log(url, body, data)
return locations
except Exception as error:
if self.Iflog:
self.log(url, body, data)
return {'': '网络错误'} # 子帐号鉴权
def subAuth(self):
if (self.ServerIP == ""):
print('')
print('IP为空') if (int(self.ServerPort) <= 0):
print('')
print('端口错误(小于等于0)') if (self.SoftVersion == ""):
print('')
print('版本号为空') if (self.SubAccountSid == ""):
print('')
print('子帐号为空') if (self.SubAccountToken == ""):
print('')
print('子帐号令牌为空') if (self.AppId == ""):
print('')
print('应用ID为空') # 主帐号鉴权
def accAuth(self):
if (self.ServerIP == ""):
print('')
print('IP为空') if (int(self.ServerPort) <= 0):
print('')
print('端口错误(小于等于0)') if (self.SoftVersion == ""):
print('')
print('版本号为空') if (self.AccountSid == ""):
print('')
print('主帐号为空') if (self.AccountToken == ""):
print('')
print('主帐号令牌为空') if (self.AppId == ""):
print('')
print('应用ID为空') # 设置包头
def setHttpHeader(self, req):
if self.BodyType == 'json':
req.add_header("Accept", "application/json")
req.add_header("Content-Type", "application/json;charset=utf-8") else:
req.add_header("Accept", "application/xml")
req.add_header("Content-Type", "application/xml;charset=utf-8")
CCPRestSDK修改后的代码
from .CCPRestSDK import REST
from django.conf import settings
# 注:在配置文件dev中完成四个配置信息
# 说明:主账号,登陆云通讯网站后,可在"控制台-应用"中看到开发者主账号ACCOUNT SID
_accountSid = settings.SMS_ACCOUNTSID
# 说明:主账号Token,登陆云通讯网站后,可在控制台-应用中看到开发者主账号AUTH TOKEN
_accountToken = settings.SMS_ACCOUNTTOKEN
# 说明:请使用管理控制台首页的APPID或自己创建应用的APPID
_appId = settings.SMS_APPID
# 说明:请求地址,生产环境配置成app.cloopen.com,开发环境配置成sandboxapp.cloopen.com
_serverIP = settings.SMS_SERVERIP # 说明:请求端口 ,生产环境为8883
_serverPort = ""
# 说明:REST API版本号保持不变
_softVersion = '2013-12-26' def send_sms(mobile, code_expire_tuple, temp_id):
# 配置
rest = REST(_serverIP, _serverPort, _softVersion)
rest.setAccount(_accountSid, _accountToken)
rest.setAppId(_appId)
# 发送
result = rest.sendTemplateSMS(mobile, code_expire_tuple, temp_id)
# 结果:信息成功发生,结果字典result中 statuCode 字段为 "000000"
if result.get("statusCode") == "":
return True # 表示发送短信成功
else:
return False # 表示发送失败
sms.py代码
django中基于python3.6使用容联发送短信的更多相关文章
- .Net core----使用容联云短信推送
一直在忙于写接口,也没有时间去好好的整理的一片文档,写一篇经常的用到的短信推送案例 还是比较喜欢干货 作为有个程序员 有时候复制粘贴习惯了 实在的还是比较实在 比如能用复制解决的为什么不用复制 ...
- Android 打开URL中的网页和拨打电话、发送短信功能
拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...
- python-在python3中使用容联云通讯发送短信验证码
容联云通讯是第三方平台,能够提供短信验证码和语音通信等功能,这里只测试使用短信验证码的功能,因此只需完成注册登录(无需实名认证等)即可使用其短信验证码免费测试服务,不过免费测试服务只能给控制台中指定的 ...
- 潭州课堂25班:Ph201805201 django 项目 第十一课 手机号是否存在,短信验证分析 (课堂笔记)
判断手机号是否注册功能实现 1.分析 请求方法:GET url定义:/mobiles/(?P<mobile>1[3-9]\d{9})/ 请求参数:url路径参数 参数 类型 前端是否必须传 ...
- Python3实现发送邮件和发送短信验证码
Python3实现发送邮件和发送短信验证码 Python3实现发送邮件: import smtplib from email.mime.text import MIMEText from email. ...
- Moto C118 基于 Osmocom-BB 和 OpenBTS 搭建小型GSM短信基站
此文章PDF文档下载地址:点击下载 0x00 写在前面 大家应该都听说过摩托罗拉C118配合Osmocom-BB实现GSM网络下的短信拦截功能吧,在14年左右新出了一种玩法就是Osmocom-BB的s ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- IOS程序开发中-跳转到 发送短信界面 实现发短信
前言:我发现我标题取的不好,谁帮我取个承接上下文的标题?评论一下,我改 项目需求:在程序开发中,我们需要在某个程序里面发送一些短信验证(不是接收短信验证,关于短信验证,传送门:http://www.c ...
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...
随机推荐
- 如何在linux中查找python安装包的路径
[root]# python -c "import fasttext;print(fasttext)"<module 'fasttext' from '/root/anaco ...
- vscode写react有warning
[js] Experimental support for decorators is a feature that is subject to change in a future release. ...
- The entity "nbsp" was referenced, but not declared
翻译 实体""被引用,但没有声明 问题 使用xhtmlrenderer将网页转成PDF时报The entity "nbsp" was referenced, b ...
- github 下载部分代码
作者:知乎用户链接:https://www.zhihu.com/question/25369412/answer/96174755来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Proto3使用指南
这篇指南讲述如何使用Protocol Buffers来结构化你的Protocol Buffer数据,包括.proto文件语法以及如何从.proto文件生成你的访问类型.本文主要涵盖了proto3的语法 ...
- Core Java之7种单例模式
初始化空 初始化创建 一把锁 两把锁 大专栏 Core Java之7种单例模式"headerlink" title="静态内部类">静态内部类 静态加载 ...
- 探索Linux通用SCSI驱动器
通过 SCSI 命令管理计算机上的数据,并将数据传输到 SCSI 设备.在本文中,作者介绍了一些 SCSI 命令,以及在 Linux® 中使用 SCSI API 时执行 SCSI 命令的方法.他介绍了 ...
- 达拉草201771010105《面向对象程序设计(java)》第十二周学习总结
达拉草201771010105<面向对象程序设计(java)>第十二周学习总结 第一部分:理论知识 Java的抽象窗口工具箱(AbstractWindow Toolkit,AWT)包含在j ...
- 小程序开发技巧(三)-- 云开发时效数据刷新和存储 (access_token等)
小程序云开发时效数据刷新和存储 (access_token等) 1.问题描述 小程序中经常有需要进行OCR识别,或者使用外部api例如百度AI识别等接口,请求调用这些接口需要令牌,即一些具有时效性的数 ...
- JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计
5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...