-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxm.py
More file actions
158 lines (128 loc) · 4.77 KB
/
mxm.py
File metadata and controls
158 lines (128 loc) · 4.77 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
MXM JSON API Client
v2.0
"""
import urlparse
import json
import socket
import base64
import urllib
import sys
import re
class Api:
def __init__(self, host, username, password, useSsl = True):
self._host = host
self._username = username
self._password = password
self._useSsl = useSsl
self._services = {}
def _getInstance(self, service):
if service not in self._services:
serviceConfig = {
'service' : service,
'host' : self._host,
'username' : self._username,
'password' : self._password,
'useSsl' : self._useSsl
}
self._services[service] = self.JsonClient(**serviceConfig)
return self._services[service]
def __getattr__(self, name):
return self._getInstance(name)
def __repr__(self):
return "%s(host='%s')" % (self.__class__, self._host)
class JsonClient:
def __init__(self, service, host, username, password, useSsl):
self._service = service
self._host = host
self._username = username
self._password = password
self._useSsl = useSsl
self._lastRequest = None
self._lastResponse = None
def getLastRequest(self):
return self._lastRequest
def getLastResponse(self):
return self._lastResponse
def _postRequest(self, data):
host = ('ssl://' if self._useSsl else '') + self._host
port = 443 if self._useSsl else 80
s = self._createSocket(host, port)
basicAuth = base64.b64encode(self._username + ':' + self._password)
body = urllib.urlencode(data)
headers = {
'Host' : self._host,
'Connection' : 'close',
'Authorization' : "Basic %s" % (basicAuth),
'Content-type' : 'application/x-www-form-urlencoded',
'Content-length' : len(body),
'User-Agent' : "MxmJsonClient/2.0 Python/"
}
request = "POST /api/json/%s HTTP/1.0\r\n" % (self._service)
for key in headers:
request += "%s: %s\r\n" % (key, headers[key])
request += "\r\n%s" % (body)
self._lastRequest = request
s.sendall(request)
return self._handleResponse(s)
def _handleResponse(self, s):
response = ''
while 1:
data = s.recv(1024)
if not data: break
response += data
s.close()
self._lastResponse = response
matches = re.match("^HTTP\/[\d\.x]+ (\d+)", response)
code = int(matches.group(1))
parts = re.split('(?:\r?\n){2}', response, 2)
content = parts[1]
if code != 200:
try:
message = self._decodeJson(content)
if 'msg' in message:
content = message['msg']
except Exception:
pass
raise Exception("%s (%d)" % (content, code))
return content
def _createSocket(self, host, port):
s = None
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
continue
try:
s.connect(sa)
except socket.error as msg:
s.close()
s = None
continue
break
if s is None:
raise Exception('Could not open socket')
return s
def _decodeJson(self, string):
try:
result = json.loads(string)
except Exception, e:
raise Exception('Problem decoding json (%s), %s' % (string, e))
return result
def __getattr__(self, name):
data = {'method': name}
def function(*args):
i = 0
for arg in args:
if (isinstance(arg, (list, tuple, dict))):
data['arg' + str(i)] = json.dumps(arg)
else:
data['arg' + str(i)] = arg
i += 1
result = self._postRequest(data)
return self._decodeJson(result)
return function
def __repr__(self):
return "%s(service='%s',host='%s')" % (self.__class__, self._service, self._host)