-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyxmlrpclib.py
executable file
·108 lines (86 loc) · 2.29 KB
/
pyxmlrpclib.py
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
#!/usr/bin/python -O
#
# A fast replacement for xmlrpclib
#
#
import _xmlrpc
# begin by importing all available functions
#
from xmlrpclib import *
# override xmlrpclib classes with _xmlrpc alternatives
#
Boolean = _xmlrpc.boolean
DateTime = _xmlrpc.dateTime
Binary = _xmlrpc.base64
# override the Marshaller class with a (much) simpler version
#
class Marshaller:
def __init__(self):
pass
def dumps(self, values):
if ((isinstance(values, Fault))
or (isinstance(values, Fault))):
d = { 'faultString' : values.faultString,
'faultCode' : values.faultCode }
return ('<fault>\n%s</fault>\n' % _xmlrpc.encode(d))
else:
l = ['<params>\n']
for item in values:
l.append('\t<param>\n\t\t')
l.append(_xmlrpc.encode(item))
l.append('\n\t</param>\n')
l.append('</params>\n')
return string.join(l, '')
# override the Marshaller class with a (much) faster version
#
class Parser:
def __init__(self, target):
self.result = None
self.target = target
self.file = StringIO()
def feed(self, s):
self.file.write(s)
def close(self):
v = self.file.getvalue()
self.target.set_data(v)
# ensure that we use our fast parser
#
FastParser = Parser
# override the Marshaller class with a (much) faster version
#
class Unmarshaller:
def __init__(self):
self.data = None
self.method = None
self.value = None
def set_data(self, data):
self.data = data
def close(self):
s = string.lstrip(self.data)
if s[:7] == '<value>':
self.value = _xmlrpc.decode(data)
elif s[:14] == "<?xml version=":
i = string.find(s, '?>')
if i < 0:
raise TypeError("unrecognized data: %.40s..." % s)
s = string.lstrip(s[i+2:])
if s[:16] == '<methodResponse>':
try:
s = ("HTTP/1.0 200 OK\r\n"
"Content-length: %d\r\n\r\n"
"%s" % (len(self.data),self.data))
self.value = _xmlrpc.parseResponse(s)[0]
except _xmlrpc.fault:
v = sys.exc_value
raise Fault(v.faultCode, v.faultString)
elif s[:12] == '<methodCall>':
(self.method, self.value) = _xmlrpc.parseCall(self.data)
if self.value == None:
raise TypeError("unrecognized data: %.40s..." % s)
return self.value
def getmethodname(self):
return self.method
if __name__ == "__main__":
server = Server('http://localhost:23456')
print(server)
print(server.echo('blah'))