def sendTemplateSMS(self, to, datas, tempId): """ 发送模板短信 :param to: 必选参数 短信接收彿手机号码集合,用英文逗号分开 :param datas: 可选参数 内容数据 :param tempId: 必选参数 模板Id """ 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://{ServerIP}:{ServerPort}/{SoftVersion}/Accounts/{AccountSid}/SMS/TemplateSMS?sig={sig}".format( ServerIP=self.ServerIP, ServerPort=self.ServerPort, SoftVersion=self.SoftVersion, AccountSid=self.AccountSid, 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) # 创建包体 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': b = '[' for a in datas: b += '"%s",' % a b += ']' body = '''{"to": "%s", "datas": %s, "templateId": "%s", "appId": "%s"}''' % ( to, b, tempId, self.AppId) req.add_data(body.encode('utf-8')) data = '' try: res = urllib2.urlopen(req) data = res.read() res.close() if self.BodyType == 'json': locations = json.loads(data) else: 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': '网络错误'}
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.new(signature).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() #http_client = AsyncHTTPClient() #req = yield http_client.fetch(url) 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.add_data(body) 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 #yield locations except Exception, error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
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.new(signature).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() 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.add_data(body) 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, error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
def callBack(self, fromPhone, to, customerSerNum, fromSerNum, promptTone, alwaysPlay, terminalDtmf, userData, maxCallTime, hangupCdrUrl, needBothCdr, needRecord, countDownTime, countDownPrompt): self.subAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.SubAccountSid + self.SubAccountToken + self.Batch sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://" + self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/SubAccounts/" + self.SubAccountSid + "/Calls/Callback?sig=" + sig #生成auth src = self.SubAccountSid + ":" + 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"?><CallBack>\ <from>%s</from><to>%s</to><customerSerNum>%s</customerSerNum><fromSerNum>%s</fromSerNum><promptTone>%s</promptTone><userData>%s</userData><maxCallTime>%s</maxCallTime><hangupCdrUrl>%s</hangupCdrUrl>\ <alwaysPlay>%s</alwaysPlay><terminalDtmf>%s</terminalDtmf><needBothCdr>%s</needBothCdr><needRecord>%s</needRecord><countDownTime>%s</countDownTime><countDownPrompt>%s</countDownPrompt>\ </CallBack>\ ''' % (fromPhone, to, customerSerNum, fromSerNum, promptTone, userData, maxCallTime, hangupCdrUrl, alwaysPlay, terminalDtmf, needBothCdr, needRecord, countDownTime, countDownPrompt) if self.BodyType == 'json': body = '''{"from": "%s", "to": "%s","customerSerNum": "%s","fromSerNum": "%s","promptTone": "%s","userData": "%s","maxCallTime": "%s","hangupCdrUrl": "%s","alwaysPlay": "%s","terminalDtmf": "%s","needBothCdr": "%s","needRecord": "%s","countDownTime": "%s","countDownPrompt": "%s"}''' % ( fromPhone, to, customerSerNum, fromSerNum, promptTone, userData, maxCallTime, hangupCdrUrl, alwaysPlay, terminalDtmf, needBothCdr, needRecord, countDownTime, countDownPrompt) req.add_data(body) 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, error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
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.new(signature).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() 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"?><TemplateSMS><datas>'+b+'</datas><to>%s</to><templateId>%s</templateId><appId>%s</appId>\ # </TemplateSMS>\ # '%(to, tempId,self.AppId) body = '<?xml version="1.0" encoding="utf-8"?><TemplateSMS><datas>{}</datas><to>{}</to><templateId>{}</templateId><appId>{}</appId></TemplateSMS>'.format(b,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.add_header("Content-Length", len(body)) req.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
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.new(signature).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() 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.add_data(body) 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, error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
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 {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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 {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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 {'172001': '网络错误'}
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 as error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.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':'网络错误'}
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 {'172001': '网络错误'}
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.new(signature).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() 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.add_data(body) 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, error: if self.Iflog: self.log(url, body, data) return {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
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 self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
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 {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def callBack(self,fromPhone,to,customerSerNum,fromSerNum,promptTone,alwaysPlay,terminalDtmf,userData,maxCallTime,hangupCdrUrl,needBothCdr,needRecord,countDownTime,countDownPrompt): self.subAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.SubAccountSid + self.SubAccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/SubAccounts/" + self.SubAccountSid + "/Calls/Callback?sig=" + sig #生成auth src = self.SubAccountSid + ":" + 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"?><CallBack>\ <from>%s</from><to>%s</to><customerSerNum>%s</customerSerNum><fromSerNum>%s</fromSerNum><promptTone>%s</promptTone><userData>%s</userData><maxCallTime>%s</maxCallTime><hangupCdrUrl>%s</hangupCdrUrl>\ <alwaysPlay>%s</alwaysPlay><terminalDtmf>%s</terminalDtmf><needBothCdr>%s</needBothCdr><needRecord>%s</needRecord><countDownTime>%s</countDownTime><countDownPrompt>%s</countDownPrompt>\ </CallBack>\ '''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl,alwaysPlay,terminalDtmf,needBothCdr,needRecord,countDownTime,countDownPrompt) if self.BodyType == 'json': body = '''{"from": "%s", "to": "%s","customerSerNum": "%s","fromSerNum": "%s","promptTone": "%s","userData": "%s","maxCallTime": "%s","hangupCdrUrl": "%s","alwaysPlay": "%s","terminalDtmf": "%s","needBothCdr": "%s","needRecord": "%s","countDownTime": "%s","countDownPrompt": "%s"}'''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl,alwaysPlay,terminalDtmf,needBothCdr,needRecord,countDownTime,countDownPrompt) req.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def callBack(self,fromPhone,to,customerSerNum,fromSerNum,promptTone): self.subAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.SubAccountSid + self.SubAccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/SubAccounts/" + self.SubAccountSid + "/Calls/Callback?sig=" + sig #生成auth src = self.SubAccountSid + ":" + 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"?><CallBack>\ <from>%s</from><to>%s</to><customerSerNum>%s</customerSerNum><fromSerNum>%s</fromSerNum><promptTone>%s</promptTone>\ </CallBack>\ '''%(fromPhone, to,customerSerNum,fromSerNum,promptTone) if self.BodyType == 'json': body = '''{"from": "%s", "to": "%s","customerSerNum": "%s","fromSerNum": "%s","promptTone": "%s"}'''%(fromPhone, to,customerSerNum,fromSerNum,promptTone) req.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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 {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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.add_data(body) 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
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.new(signature).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.encodestring(src).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 {'172001': '网络错误'}
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.new(signature).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.encodestring(src).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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
url = "http://218.17.248.244:11080/bsp-oisp/sfexpressService" data = {'xml': m, 'verifyCode': auth} req = urllib2.Request(url) da = urllib.urlencode(data) try: response = urllib2.urlopen(req, da) except urllib2.HTTPError, e: print e.code print e.read() a = response.read() print(a) # xtj = xmltojson() # locations = xtj.main(a) # print(m) xtj = xmltojson() locations = xtj.get_root(a) # print(xtj.get_element_children(locations)) ass = xtj.get_element_children(locations) threenodes = xtj.get_element_children(ass[1]) myattes = xtj.get_element_attrib(threenodes[0]) threeno = xtj.get_element_children(threenodes[0]) ss = xtj.get_element_attrib(threeno[0]) print(ss["remark"]) # print(url) # req = urllib2.Request(url) # res = urllib2.urlopen(req); # data = res.read() # print(data)
pointList = [] for i in range(np.shape(maskPoints[j])[0]): point = { 'X': (maskPoints[j][i][0][0] * downsample_factor) + offset['X'], 'Y': (maskPoints[j][i][0][1] * downsample_factor) + offset['Y'] } pointList.append(point) pointsList.append(pointList) Annotations = ET.Element('Annotations', attrib={'MicronsPerPixel': '0.136031'}) col1 = str(65280) Annotations = FNs.xml_add_annotation(Annotations=Annotations, annotationID=1, LC=col1) for i in range(np.shape(pointsList)[0]): pointList = pointsList[i] Annotations = FNs.xml_add_region(Annotations=Annotations, pointList=pointList) xml_data = ET.tostring(Annotations, pretty_print=True) f = open(args.outxml1, 'wb') f.write(xml_data) f.close() tree = ET.parse(args.outxml1) root = tree.getroot() json_data = xmltojson(root) with open(args.jsonout, 'w') as annotation_file: json.dump(json_data, annotation_file, indent=2, sort_keys=False)
def GenerateXml(): impl = xml.dom.minidom.getDOMImplementation() dom = impl.createDocument(None, None, None) #生成request节点 root = dom.createElement("Request") #添加属性 root.setAttribute("service", "OrderService") #增加属性 root.setAttribute("lang", "zh-CN") #增加属性 dom.appendChild(root) #生成head节点 head = dom.createElement("Head") headvalue = dom.createTextNode('PLY') head.appendChild(headvalue) root.appendChild(head) #生成Body节点 body = dom.createElement("Body") root.appendChild(body) #body节点下生成order节点,保存传递订单信息 ordernode = dom.createElement("Order") #订单号 ordernode.setAttribute("orderid", "my00003") # #寄件公司名称 # ordernode.setAttribute("j_company", "") # #寄件方联系人 # ordernode.setAttribute("j_contact", "") # #寄件方联系电话 # ordernode.setAttribute("j_tel", "") # #寄件方手机 ordernode.setAttribute("j_mobile", "18620558165") # #寄件方所在省份 # ordernode.setAttribute("j_province", "") # #寄件方所在城市 # ordernode.setAttribute("j_city", "") # #寄件人所在县区 # ordernode.setAttribute("j_county", "") # #寄件方详细地址 ordernode.setAttribute("j_address", "广州市大学城北国家数字基地创业楼A401") #到件方公司名称 ordernode.setAttribute("d_company", "天食地力") #到件方联系人 ordernode.setAttribute("d_contact", "天食地力") #到件方联系电话 ordernode.setAttribute("d_tel", "18620558165") #到件方手机 ordernode.setAttribute("d_mobile", "18620558165") #到件方详细地址 ordernode.setAttribute("d_address", "广州市大学城北国家数字基地创业楼A401") #快件产品类别 ordernode.setAttribute("express_type", "3") #付款方式 ordernode.setAttribute("pay_method", "1") #包裹数 ordernode.setAttribute("parcel_quantity", "1") #备注 #ordernode.setAttribute("remark", "sdsdasdasdasdas") body.appendChild(ordernode) addmonnode = dom.createElement("AddedService") addmonnode.setAttribute("name", "COD") addmonnode.setAttribute("value", '198.00') addmonnode.setAttribute("value1", "6236683320006442636") ordernode.appendChild(addmonnode) m = dom.toxml('UTF-8') # ms = "<Request service='OrderService' lang='zh-CN'><Head>BSPdevelop</Head><Body><Order orderid='10000001' express_type='1' j_province='广东省' j_city='深圳市' j_company='顺丰速运' j_contact='喵小萌' j_tel='95338' j_address='广东省深圳市福田区新洲十一街' d_province='北京' d_city='北京' d_company='一家公司' d_contact='萌小喵' d_tel='18888998899' d_address='北京市海淀区科学园科健路908' parcel_quantity='1' pay_method='1' custid='7551878519' ><Cargo name='服装' count='1' unit='台' weight='2.36' amount='2000' currency='CNY' source_area='中国'></Cargo></Order></Body></Request>" checkword = "lsxf0h0HVVn9aHli" mymd = hashlib.md5() mymd.update(m + checkword) sig = mymd.digest() # sig = md5.new(ms + checkword).hexdigest() # auth = base64.encodestring(sig) auth = base64.b64encode(sig) print(auth) url = "http://218.17.248.244:11080/bsp-oisp/sfexpressService" data = {'xml': m, 'verifyCode': auth} req = urllib2.Request(url) da = urllib.urlencode(data) response = urllib2.urlopen(req, da) a = response.read() print(a) # xtj = xmltojson() # locations = xtj.main(a) # print(locations["ERROR"]) # locations["Head"] # print(m) xtj = xmltojson() print(xtj) backdom = xtj.get_root(a) twonodes = xtj.get_element_children(backdom) threenodes = xtj.get_element_children(twonodes[1]) myattr = xtj.get_element_attrib(threenodes[0]) print(myattr["orderid"]) print(myattr["mailno"])
from datetime import datetime def str_to_time(date_str): """ converts the date string to datetime object """ date_fmt = "%a, %d %b %Y %H:%M:%S" return datetime.strptime(date_str, date_fmt) db.connect('sentinel') class Report(db.Document): """ a report document """ OWASPZAPReport = db.DynamicField() def insert(self, json, collection='newreport'): """ inserts the report to the database """ self.OWASPZAPReport= json['OWASPZAPReport'] self.switch_collection(collection) self.save() xmlreport = "full-report.xml" json_report = xmltojson(xmlreport) report = Report() report_collection = 'ipc' #report.insert(json_report, report_collection) report.insert(json_report)
def GetyuantongXml(): impl = xml.dom.minidom.getDOMImplementation() dom = impl.createDocument(None, None, None) #生成request节点 root = dom.createElement("RequestOrder") dom.appendChild(root) #生成clientID节点 clientID = dom.createElement("clientID") clientIDval = dom.createTextNode('TEST') clientID.appendChild(clientIDval) root.appendChild(clientID) #生成logisticProviderID节点 logisticProviderID = dom.createElement("logisticProviderID") logisticProviderIDval = dom.createTextNode("YTO") logisticProviderID.appendChild(logisticProviderIDval) root.appendChild(logisticProviderID) #生成txLogisticID节点(clientid+订单号) txLogisticID = dom.createElement("txLogisticID") txLogisticIDval = dom.createTextNode("LP07082300225709") txLogisticID.appendChild(txLogisticIDval) root.appendChild(txLogisticID) # #生成tradeNo节点 # tradeNo = dom.createElement("tradeNo") # tradeNoval = dom.createTextNode("2007082300225709") # tradeNo.appendChild(tradeNoval) # root.appendChild(tradeNo) #生成mailNo节点 mailNo = dom.createElement("mailNo") mailNoval = dom.createTextNode("124579546621") mailNo.appendChild(mailNoval) root.appendChild(mailNo) # #生成totalServiceFee节点 # totalServiceFee = dom.createElement("totalServiceFee") # totalServiceFeeval = dom.createTextNode("0.0") # totalServiceFee.appendChild(totalServiceFeeval) # root.appendChild(totalServiceFee) # #生成codSplitFee节点 # codSplitFee = dom.createElement("codSplitFee") # codSplitFeeval = dom.createTextNode("0.0") # codSplitFee.appendChild(codSplitFeeval) # root.appendChild(codSplitFee) #生成orderType节点 orderType = dom.createElement("orderType") orderTypeval = dom.createTextNode("1") orderType.appendChild(orderTypeval) root.appendChild(orderType) #生成serviceType节点 serviceType = dom.createElement("serviceType") serviceTypeval = dom.createTextNode("0") serviceType.appendChild(serviceTypeval) root.appendChild(serviceType) #生成flag节点 # flag = dom.createElement("flag") # flagval = dom.createTextNode("0") # flag.appendChild(flagval) # root.appendChild(flag) """ 生成发货方信息sender节点 """ sender = dom.createElement("sender") root.appendChild(sender) #生成name name = dom.createElement("name") nameval = dom.createTextNode("张三") name.appendChild(nameval) sender.appendChild(name) #生成postCode postCode = dom.createElement("postCode") postCodeval = dom.createTextNode("510000") postCode.appendChild(postCodeval) sender.appendChild(postCode) #生成mobile mobile = dom.createElement("mobile") mobileval = dom.createTextNode("18620558165") mobile.appendChild(mobileval) sender.appendChild(mobile) #生成prov prov = dom.createElement("prov") provval = dom.createTextNode("广东省") prov.appendChild(provval) sender.appendChild(prov) #生成city city = dom.createElement("city") cityval = dom.createTextNode("广州市") city.appendChild(cityval) sender.appendChild(city) #生成address address = dom.createElement("address") addressval = dom.createTextNode("白云区天河北路123号") address.appendChild(addressval) sender.appendChild(address) """ 收货方信息receiver节点 """ receiver = dom.createElement("receiver") root.appendChild(receiver) #生成name name = dom.createElement("name") nameval = dom.createTextNode("张三") name.appendChild(nameval) receiver.appendChild(name) #生成postCode postCode = dom.createElement("postCode") postCodeval = dom.createTextNode("310013") postCode.appendChild(postCodeval) receiver.appendChild(postCode) #生成mobile mobile = dom.createElement("mobile") mobileval = dom.createTextNode("18620558165") mobile.appendChild(mobileval) receiver.appendChild(mobile) #生成prov prov = dom.createElement("prov") provval = dom.createTextNode("广东省") prov.appendChild(provval) receiver.appendChild(prov) #生成city city = dom.createElement("city") cityval = dom.createTextNode("广州市") city.appendChild(cityval) receiver.appendChild(city) #生成address address = dom.createElement("address") addressval = dom.createTextNode("白云区天河北路123号") address.appendChild(addressval) receiver.appendChild(address) """ 添加商品属性 """ items = dom.createElement("items") root.appendChild(items) item = dom.createElement("item") items.appendChild(item) #生成itemName itemName = dom.createElement("itemName") itemNameval = dom.createTextNode("智能手表") itemName.appendChild(itemNameval) item.appendChild(itemName) #生成itemName number = dom.createElement("number") numberval = dom.createTextNode("1") number.appendChild(numberval) item.appendChild(number) #生成special special = dom.createElement("special") specialval = dom.createTextNode("0") special.appendChild(specialval) root.appendChild(special) m = dom.toxml('UTF-8') # ms = "<Request service='OrderService' lang='zh-CN'><Head>BSPdevelop</Head><Body><Order orderid='10000001' express_type='1' j_province='广东省' j_city='深圳市' j_company='顺丰速运' j_contact='喵小萌' j_tel='95338' j_address='广东省深圳市福田区新洲十一街' d_province='北京' d_city='北京' d_company='一家公司' d_contact='萌小喵' d_tel='18888998899' d_address='北京市海淀区科学园科健路908' parcel_quantity='1' pay_method='1' custid='7551878519' ><Cargo name='服装' count='1' unit='台' weight='2.36' amount='2000' currency='CNY' source_area='中国'></Cargo></Order></Body></Request>" partnerId = "123456" mymd = hashlib.md5() mymd.update(m + partnerId) sig = mymd.digest() # sig = md5.new(ms + checkword).hexdigest() # auth = base64.encodestring(sig) auth = base64.b64encode(sig) print(auth) # urlm = urllib.quote(m) url = "http://58.32.246.92:9081/ordws/Vip16Servlet" data = {'logistics_interface': m, 'data_digest': auth, 'type': 'offline', 'clientId': 'TEST'} req = urllib2.Request(url) da = urllib.urlencode(data) response = urllib2.urlopen(req, da) a = response.read() print(a) # xtj = xmltojson() # locations = xtj.main(a) # print(locations["ERROR"]) # locations["Head"] # print(m) xtj = xmltojson() print(xtj) backdom = xtj.main(a) print(backdom["success"])
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.new(signature).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() # req = urllib2.Request(url) # self.setHttpHeader(req) # req.add_header("Authorization", auth) request = tornado.httpclient.HTTPRequest() headers = {} if self.BodyType == 'json': headers = { "Accept": "application/json", "Content-Type": "application/json;charset=utf-8", "Authorization": auth } else: # req.add_header("Accept", "application/xml") # req.add_header("Content-Type", "application/xml;charset=utf-8") headers = { "Accept": "application/xml", "Content-Type": "application/xml;charset=utf-8", "Authorization": auth } request.headers(headers) #创建包体 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) request.body = body 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 h = tornado.httpclient.AsyncHTTPClient() response = yield h.fetch(url) if self.BodyType == 'json': data = json.loads(response.body) else: # #xml格式 xtj = xmltojson() data = xtj.main(response.body) if self.Iflog: self.log(url, body, response.body) raise tornado.gen.Return(data) except Exception, error: if self.Iflog: self.log(url, body, response.body) raise tornado.gen.Return({'172001': '网络错误'})