#-*- 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.
import md5
import base64
import datetime
import urllib2
import json
from xmltojson import xmltojson
from xml.dom import minidom
class REST:
AccountSid=''
AccountToken=''
AppId=''
SubAccountSid=''
SubAccountToken=''
ServerIP=''
ServerPort=''
SoftVersion=''
Iflog=True #是否打印日志
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.new(signature).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.encodestring(src).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.add_data(body)
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, error:
if self.Iflog:
self.log(url,body,data)
return {'172001':'网络错误'}
# 获取子帐号
# @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.new(signature).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()
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.add_data(body)
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, error:
if self.Iflog:
self.log(url,body,data)
return {'172001':'网络错误'}
# 子帐号信息查询
# @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.new(signature).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()
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.add_data(body)
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, error:
if s
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目是使用python+tornado4.4.3版本 将项目文件下载到你的文件目录,创建一个新的虚拟环境,并在虚拟环境下执行如下命令安装项目需要的模块。 pip install -r requirements.txt 进入项目根目录,执行python server.py --runserver默认开启本地的8000端口 打开浏览器在地址栏中输入http://127.0.0.1:8000/auth/user_regist命令打开项目,并注册登录,
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计,课程设计-基于Tornado做的有登录注册、新闻发布、评论、权限管理、分享、OA信息发布、实时聊天等功能的管理系统 (1110个子文件)
COPYING 2KB
bootstrap.css 118KB
bootstrap.min.css 98KB
style.css 83KB
animate.css 72KB
editor_ie7.css 29KB
editor_iequirks.css 28KB
editor_ie8.css 27KB
editor_ie.css 27KB
simditor.css 27KB
editor_gecko.css 26KB
editor.css 26KB
sweetalert.css 22KB
_all.css 21KB
font-awesome.css 21KB
font-awesome.css 21KB
jquery-ui-1.10.3.css 18KB
font-awesome.min.css 17KB
dialog_ie7.css 16KB
dialog_ie8.css 16KB
dialog_iequirks.css 16KB
dialog_ie.css 16KB
dialog_opera.css 15KB
dialog.css 15KB
_all.css 15KB
_all.css 15KB
_all.css 13KB
datetimepicker-custom.css 12KB
datetimepicker.css 12KB
datetimepicker.css 12KB
bootstrap-fullcalendar.css 12KB
demo_table.css 11KB
fullcalendar.css 10KB
dropzone.css 10KB
demo_table_jui.css 8KB
clndr.css 6KB
style-responsive.css 5KB
sample.css 5KB
datepicker-custom.css 5KB
daterangepicker-bs3.css 5KB
tree-style.css 5KB
jquery.dataTables_themeroller.css 4KB
jquery.dataTables.css 4KB
datepicker.css 4KB
jquery.nestable.css 4KB
daterangepicker.css 4KB
DT_bootstrap.css 4KB
bootstrap-reset.css 3KB
basic.css 3KB
table-responsive.css 3KB
ion.rangeSlider.css 3KB
bootstrap-fileupload.min.css 3KB
normalize.min.css 2KB
multi-select.css 2KB
bootstrap-wysihtml5.css 2KB
colorpicker.css 2KB
yellow.css 2KB
purple.css 2KB
orange.css 2KB
green.css 2KB
grey.css 2KB
pink.css 2KB
aero.css 2KB
blue.css 2KB
red.css 2KB
outputxhtml.css 2KB
line.css 2KB
timepicker.css 2KB
timepicker.css 2KB
jquery.gritter.css 2KB
ion.rangeSlider.skinFlat.css 2KB
tree.css 2KB
demo_page.css 2KB
c3.css 2KB
ion.rangeSlider.skinSimple.css 2KB
ion.rangeSlider.skinNice.css 2KB
purple.css 2KB
orange.css 2KB
yellow.css 2KB
purple.css 2KB
orange.css 2KB
yellow.css 2KB
green.css 2KB
green.css 2KB
pink.css 2KB
grey.css 2KB
aero.css 2KB
blue.css 2KB
all.css 2KB
pink.css 2KB
blue.css 2KB
grey.css 2KB
aero.css 2KB
red.css 2KB
red.css 2KB
minimal.css 1KB
polaris.css 1KB
jquery.stepy.css 1KB
square.css 1KB
contents.css 1KB
共 1110 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
十小大
- 粉丝: 9954
- 资源: 2555
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功