-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamiXml2Array.py
More file actions
82 lines (65 loc) · 2.35 KB
/
amiXml2Array.py
File metadata and controls
82 lines (65 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cookielib
import urllib
import urllib2
import xml.etree.ElementTree as elements
import string
class AmiXml2Array:
def __init__(self,globalUrl,loginUrl):
self.globalUrl = globalUrl
self.loginUrl = loginUrl
def login_ami_http(self,loginUrl):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
con = urllib2.Request(loginUrl)
response = urllib2.urlopen(con)
return response.read()
def logoff_ami_http(self,globalUrl):
con = urllib2.Request(self.globalUrl+'action=logoff')
response = urllib2.urlopen(con)
return response.read()
def get_simple_action(self,action):
self.login_ami_http(self.loginUrl)
url = self.globalUrl+'action='+action;
con = urllib2.Request(url)
response = urllib2.urlopen(con)
self.logoff_ami_http(self.globalUrl)
xml_nodes = response.read()
json = self.parse_xml(xml_nodes)
return json
def get_queue_status_by_name(self, queueName):
self.login_ami_http(self.loginUrl)
url = self.globalUrl+'action=QueueStatus&queue='+queueName
con = urllib2.Request(url)
response = urllib2.urlopen(con)
self.logoff_ami_http(self.globalUrl)
xml_nodes = response.read()
json = self.parse_xml(xml_nodes)
return json
def get_sipstatus_by_sip_peer(self,sipPeeer):
self.login_ami_http(self.loginUrl)
url = self.globalUrl+'action=SIPshowpeer&peer='+str(sipPeeer)
con = urllib2.Request(url)
response = urllib2.urlopen(con)
self.logoff_ami_http(self.globalUrl)
xml_nodes = response.read()
json = self.parse_xml(xml_nodes)
return json
def parse_xml(self,xmlString):
newsitems = []
itens = elements.fromstring(xmlString)
# root = itens.getroot()
for item in itens.findall('./response'):
for child in item:
newChild = string.replace(str(child.attrib),'"','')
newsitems.append(newChild)
outPutFormat = string.replace(str(newsitems),'"{','{')
outPutFormat = string.replace(outPutFormat,'}"','}')
return string.replace(outPutFormat,'\'','"')
globalUrl = 'http://192.168.1.10:8088/mxml?'
loginUrl = 'http://192.168.1.10:8088/mxml?action=login&username=asterisk&secret=asterisk'
ami = AmiXml2Array(globalUrl,loginUrl)
#Exemplo de utilizacao
print(ami.get_simple_action('QueueStatus'))
print(ami.get_queue_status_by_name('fila_teste'))
print(ami.get_sipstatus_by_sip_peer('11004'))