blob: 0a9168387acf65084ea1b953ce00fdfbe6b3e179 [file] [log] [blame]
Fredrik Lundhb329b712001-09-17 17:35:21 +00001"""Simple XML-RPC Server.
2
3This module can be used to create simple XML-RPC servers
4by creating a server and either installing functions, a
5class instance, or by extending the SimpleXMLRPCRequestHandler
6class.
7
8A list of possible usage patterns follows:
9
101. Install functions:
11
12server = SimpleXMLRPCServer(("localhost", 8000))
13server.register_function(pow)
14server.register_function(lambda x,y: x+y, 'add')
15server.serve_forever()
16
172. Install an instance:
18
19class MyFuncs:
20 def __init__(self):
21 # make all of the string functions available through
22 # string.func_name
23 import string
24 self.string = string
25 def pow(self, x, y): return pow(x, y)
26 def add(self, x, y) : return x + y
27server = SimpleXMLRPCServer(("localhost", 8000))
28server.register_instance(MyFuncs())
29server.serve_forever()
30
313. Install an instance with custom dispatch method:
32
33class Math:
34 def _dispatch(self, method, params):
35 if method == 'pow':
36 return apply(pow, params)
37 elif method == 'add':
38 return params[0] + params[1]
39 else:
40 raise 'bad method'
41server = SimpleXMLRPCServer(("localhost", 8000))
42server.register_instance(Math())
43server.serve_forever()
44
454. Subclass SimpleXMLRPCRequestHandler:
46
47class MathHandler(SimpleXMLRPCRequestHandler):
48 def _dispatch(self, method, params):
49 try:
50 # We are forcing the 'export_' prefix on methods that are
51 # callable through XML-RPC to prevent potential security
52 # problems
53 func = getattr(self, 'export_' + method)
54 except AttributeError:
55 raise Exception('method "%s" is not supported' % method)
56 else:
57 return apply(func, params)
58
59 def log_message(self, format, *args):
60 pass # maybe do something fancy like write the messages to a file
61
62 def export_add(self, x, y):
63 return x + y
64
65server = SimpleXMLRPCServer(("localhost", 8000), MathHandler)
66server.serve_forever()
67"""
68
69# Written by Brian Quinlan (brian@sweetapp.com).
70# Based on code written by Fredrik Lundh.
71
72import xmlrpclib
73import SocketServer
74import BaseHTTPServer
75import sys
76
77class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
78 """Simple XML-RPC request handler class.
79
80 Handles all HTTP POST requests and attempts to decode them as
81 XML-RPC requests.
82
83 XML-RPC requests are dispatched to the _dispatch method, which
84 may be overriden by subclasses. The default implementation attempts
85 to dispatch XML-RPC calls to the functions or instance installed
86 in the server.
87 """
88
89 def do_POST(self):
90 """Handles the HTTP POST request.
91
92 Attempts to interpret all HTTP POST requests as XML-RPC calls,
93 which are forwarded to the _dispatch method for handling.
94 """
95
96 try:
97 # get arguments
98 data = self.rfile.read(int(self.headers["content-length"]))
99 params, method = xmlrpclib.loads(data)
100
101 # generate response
102 try:
103 response = self._dispatch(method, params)
104 # wrap response in a singleton tuple
105 response = (response,)
106 except:
107 # report exception back to server
108 response = xmlrpclib.dumps(
109 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
110 )
111 else:
112 response = xmlrpclib.dumps(response, methodresponse=1)
113 except:
114 # internal error, report as HTTP server error
115 self.send_response(500)
116 self.end_headers()
117 else:
118 # got a valid XML RPC response
119 self.send_response(200)
120 self.send_header("Content-type", "text/xml")
121 self.send_header("Content-length", str(len(response)))
122 self.end_headers()
123 self.wfile.write(response)
124
125 # shut down the connection
126 self.wfile.flush()
127 self.connection.shutdown(1)
128
129 def _dispatch(self, method, params):
130 """Dispatches the XML-RPC method.
131
132 XML-RPC calls are forwarded to a registered function that
133 matches the called XML-RPC method name. If no such function
134 exists then the call is forwarded to the registered instance,
135 if available.
136
137 If the registered instance has a _dispatch method then that
138 method will be called with the name of the XML-RPC method and
139 it's parameters as a tuple
140 e.g. instance._dispatch('add',(2,3))
141
142 If the registered instance does not have a _dispatch method
143 then the instance will be searched to find a matching method
144 and, if found, will be called.
145
146 Methods beginning with an '_' are considered private and will
147 not be called by SimpleXMLRPCServer.
148 """
149
Fredrik Lundhb329b712001-09-17 17:35:21 +0000150 func = None
151 try:
152 # check to see if a matching function has been registered
153 func = self.server.funcs[method]
154 except KeyError:
155 if self.server.instance is not None:
156 # check for a _dispatch method
157 if hasattr(self.server.instance, '_dispatch'):
Fred Drake787fd8c2001-09-29 04:54:33 +0000158 return self.server.instance._dispatch(method, params)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000159 else:
160 # call instance method directly
161 try:
Fred Drake787fd8c2001-09-29 04:54:33 +0000162 func = _resolve_dotted_attribute(
Fredrik Lundhb329b712001-09-17 17:35:21 +0000163 self.server.instance,
164 method
165 )
166 except AttributeError:
167 pass
168
169 if func is not None:
170 return apply(func, params)
171 else:
172 raise Exception('method "%s" is not supported' % method)
173
174 def log_request(self, code='-', size='-'):
175 """Selectively log an accepted request."""
176
177 if self.server.logRequests:
178 BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
179
Fred Drake787fd8c2001-09-29 04:54:33 +0000180
181def _resolve_dotted_attribute(obj, attr):
182 """Resolves a dotted attribute name to an object. Raises
183 an AttributeError if any attribute in the chain starts with a '_'.
184 """
185 for i in attr.split('.'):
186 if i.startswith('_'):
187 raise AttributeError(
188 'attempt to access private attribute "%s"' % i
189 )
190 else:
191 obj = getattr(obj,i)
192 return obj
193
194
Fredrik Lundhb329b712001-09-17 17:35:21 +0000195class SimpleXMLRPCServer(SocketServer.TCPServer):
196 """Simple XML-RPC server.
197
198 Simple XML-RPC server that allows functions and a single instance
199 to be installed to handle requests.
200 """
201
202 def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
203 logRequests=1):
204 self.funcs = {}
205 self.logRequests = logRequests
206 self.instance = None
207 SocketServer.TCPServer.__init__(self, addr, requestHandler)
208
209 def register_instance(self, instance):
210 """Registers an instance to respond to XML-RPC requests.
211
212 Only one instance can be installed at a time.
213
214 If the registered instance has a _dispatch method then that
215 method will be called with the name of the XML-RPC method and
216 it's parameters as a tuple
217 e.g. instance._dispatch('add',(2,3))
218
219 If the registered instance does not have a _dispatch method
220 then the instance will be searched to find a matching method
221 and, if found, will be called.
222
223 Methods beginning with an '_' are considered private and will
224 not be called by SimpleXMLRPCServer.
225
226 If a registered function matches a XML-RPC request, then it
227 will be called instead of the registered instance.
228 """
229
230 self.instance = instance
231
232 def register_function(self, function, name = None):
233 """Registers a function to respond to XML-RPC requests.
234
235 The optional name argument can be used to set a Unicode name
236 for the function.
237
238 If an instance is also registered then it will only be called
239 if a matching function is not found.
240 """
241
242 if name is None:
243 name = function.__name__
244 self.funcs[name] = function
245
246if __name__ == '__main__':
247 server = SimpleXMLRPCServer(("localhost", 8000))
248 server.register_function(pow)
249 server.register_function(lambda x,y: x+y, 'add')
250 server.serve_forever()