blob: 9668c8cbea7e64ea7685fd60b2b9c40723b5ac58 [file] [log] [blame]
Georg Brandl38eceaa2008-05-26 11:14:17 +00001"""XML-RPC Servers.
Fredrik Lundhb329b712001-09-17 17:35:21 +00002
3This module can be used to create simple XML-RPC servers
4by creating a server and either installing functions, a
Martin v. Löwisd69663d2003-01-15 11:37:23 +00005class instance, or by extending the SimpleXMLRPCServer
Fredrik Lundhb329b712001-09-17 17:35:21 +00006class.
7
Martin v. Löwisd69663d2003-01-15 11:37:23 +00008It can also be used to handle XML-RPC requests in a CGI
9environment using CGIXMLRPCRequestHandler.
10
Georg Brandl38eceaa2008-05-26 11:14:17 +000011The Doc* classes can be used to create XML-RPC servers that
12serve pydoc-style documentation in response to HTTP
13GET requests. This documentation is dynamically generated
14based on the functions and methods registered with the
15server.
16
Fredrik Lundhb329b712001-09-17 17:35:21 +000017A list of possible usage patterns follows:
18
191. Install functions:
20
21server = SimpleXMLRPCServer(("localhost", 8000))
22server.register_function(pow)
23server.register_function(lambda x,y: x+y, 'add')
24server.serve_forever()
25
262. Install an instance:
27
28class MyFuncs:
29 def __init__(self):
Neal Norwitz9d72bb42007-04-17 08:48:32 +000030 # make all of the sys functions available through sys.func_name
31 import sys
32 self.sys = sys
Martin v. Löwisd69663d2003-01-15 11:37:23 +000033 def _listMethods(self):
34 # implement this method so that system.listMethods
Neal Norwitz9d72bb42007-04-17 08:48:32 +000035 # knows to advertise the sys methods
Martin v. Löwisd69663d2003-01-15 11:37:23 +000036 return list_public_methods(self) + \
Neal Norwitz9d72bb42007-04-17 08:48:32 +000037 ['sys.' + method for method in list_public_methods(self.sys)]
Fredrik Lundhb329b712001-09-17 17:35:21 +000038 def pow(self, x, y): return pow(x, y)
39 def add(self, x, y) : return x + y
Tim Peters2c60f7a2003-01-29 03:49:43 +000040
Fredrik Lundhb329b712001-09-17 17:35:21 +000041server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +000042server.register_introspection_functions()
Fredrik Lundhb329b712001-09-17 17:35:21 +000043server.register_instance(MyFuncs())
44server.serve_forever()
45
463. Install an instance with custom dispatch method:
47
48class Math:
Martin v. Löwisd69663d2003-01-15 11:37:23 +000049 def _listMethods(self):
50 # this method must be present for system.listMethods
51 # to work
52 return ['add', 'pow']
53 def _methodHelp(self, method):
54 # this method must be present for system.methodHelp
55 # to work
56 if method == 'add':
57 return "add(2,3) => 5"
58 elif method == 'pow':
59 return "pow(x, y[, z]) => number"
60 else:
61 # By convention, return empty
62 # string if no help is available
63 return ""
Fredrik Lundhb329b712001-09-17 17:35:21 +000064 def _dispatch(self, method, params):
65 if method == 'pow':
Martin v. Löwisd69663d2003-01-15 11:37:23 +000066 return pow(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +000067 elif method == 'add':
68 return params[0] + params[1]
69 else:
70 raise 'bad method'
Martin v. Löwisd69663d2003-01-15 11:37:23 +000071
Fredrik Lundhb329b712001-09-17 17:35:21 +000072server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +000073server.register_introspection_functions()
Fredrik Lundhb329b712001-09-17 17:35:21 +000074server.register_instance(Math())
75server.serve_forever()
76
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000774. Subclass SimpleXMLRPCServer:
Fredrik Lundhb329b712001-09-17 17:35:21 +000078
Martin v. Löwisd69663d2003-01-15 11:37:23 +000079class MathServer(SimpleXMLRPCServer):
Fredrik Lundhb329b712001-09-17 17:35:21 +000080 def _dispatch(self, method, params):
81 try:
82 # We are forcing the 'export_' prefix on methods that are
83 # callable through XML-RPC to prevent potential security
84 # problems
85 func = getattr(self, 'export_' + method)
86 except AttributeError:
87 raise Exception('method "%s" is not supported' % method)
88 else:
Martin v. Löwisd69663d2003-01-15 11:37:23 +000089 return func(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +000090
91 def export_add(self, x, y):
92 return x + y
93
Martin v. Löwisd69663d2003-01-15 11:37:23 +000094server = MathServer(("localhost", 8000))
Fredrik Lundhb329b712001-09-17 17:35:21 +000095server.serve_forever()
Martin v. Löwisd69663d2003-01-15 11:37:23 +000096
975. CGI script:
98
99server = CGIXMLRPCRequestHandler()
100server.register_function(pow)
101server.handle_request()
Fredrik Lundhb329b712001-09-17 17:35:21 +0000102"""
103
104# Written by Brian Quinlan (brian@sweetapp.com).
105# Based on code written by Fredrik Lundh.
106
Georg Brandl38eceaa2008-05-26 11:14:17 +0000107from xmlrpc.client import Fault, dumps, loads
Alexandre Vassalottice261952008-05-12 02:31:37 +0000108import socketserver
Fredrik Lundhb329b712001-09-17 17:35:21 +0000109import BaseHTTPServer
110import sys
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111import os
Georg Brandl38eceaa2008-05-26 11:14:17 +0000112import re
113import pydoc
114import inspect
Guido van Rossum61e21b52007-08-20 19:06:03 +0000115import traceback
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116try:
117 import fcntl
118except ImportError:
119 fcntl = None
Fredrik Lundhb329b712001-09-17 17:35:21 +0000120
Guido van Rossumd0641422005-02-03 15:01:24 +0000121def resolve_dotted_attribute(obj, attr, allow_dotted_names=True):
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000122 """resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d
Fredrik Lundhb329b712001-09-17 17:35:21 +0000123
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000124 Resolves a dotted attribute name to an object. Raises
125 an AttributeError if any attribute in the chain starts with a '_'.
Guido van Rossumd0641422005-02-03 15:01:24 +0000126
127 If the optional allow_dotted_names argument is false, dots are not
128 supported and this function operates similar to getattr(obj, attr).
Fredrik Lundhb329b712001-09-17 17:35:21 +0000129 """
130
Guido van Rossumd0641422005-02-03 15:01:24 +0000131 if allow_dotted_names:
132 attrs = attr.split('.')
133 else:
134 attrs = [attr]
135
136 for i in attrs:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000137 if i.startswith('_'):
138 raise AttributeError(
139 'attempt to access private attribute "%s"' % i
140 )
141 else:
142 obj = getattr(obj,i)
143 return obj
Fredrik Lundhb329b712001-09-17 17:35:21 +0000144
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000145def list_public_methods(obj):
146 """Returns a list of attribute strings, found in the specified
147 object, which represent callable attributes"""
148
149 return [member for member in dir(obj)
150 if not member.startswith('_') and
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000151 hasattr(getattr(obj, member), '__call__')]
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000152
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000153class SimpleXMLRPCDispatcher:
154 """Mix-in class that dispatches XML-RPC requests.
155
156 This class is used to register XML-RPC method handlers
157 and then to dispatch them. There should never be any
158 reason to instantiate this class directly.
159 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000160
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +0000161 def __init__(self, allow_none, encoding):
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000162 self.funcs = {}
163 self.instance = None
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +0000164 self.allow_none = allow_none
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +0000165 self.encoding = encoding
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000166
Guido van Rossumd0641422005-02-03 15:01:24 +0000167 def register_instance(self, instance, allow_dotted_names=False):
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000168 """Registers an instance to respond to XML-RPC requests.
169
170 Only one instance can be installed at a time.
171
172 If the registered instance has a _dispatch method then that
173 method will be called with the name of the XML-RPC method and
Georg Brandl7eb4b7d2005-07-22 21:49:32 +0000174 its parameters as a tuple
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000175 e.g. instance._dispatch('add',(2,3))
176
177 If the registered instance does not have a _dispatch method
178 then the instance will be searched to find a matching method
179 and, if found, will be called. Methods beginning with an '_'
180 are considered private and will not be called by
181 SimpleXMLRPCServer.
182
183 If a registered function matches a XML-RPC request, then it
184 will be called instead of the registered instance.
Guido van Rossumd0641422005-02-03 15:01:24 +0000185
186 If the optional allow_dotted_names argument is true and the
187 instance does not have a _dispatch method, method names
188 containing dots are supported and resolved, as long as none of
189 the name segments start with an '_'.
190
191 *** SECURITY WARNING: ***
192
193 Enabling the allow_dotted_names options allows intruders
194 to access your module's global variables and may allow
195 intruders to execute arbitrary code on your machine. Only
196 use this option on a secure, closed network.
197
Fredrik Lundhb329b712001-09-17 17:35:21 +0000198 """
199
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000200 self.instance = instance
Guido van Rossumd0641422005-02-03 15:01:24 +0000201 self.allow_dotted_names = allow_dotted_names
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000202
203 def register_function(self, function, name = None):
204 """Registers a function to respond to XML-RPC requests.
205
206 The optional name argument can be used to set a Unicode name
207 for the function.
208 """
209
210 if name is None:
211 name = function.__name__
212 self.funcs[name] = function
213
214 def register_introspection_functions(self):
215 """Registers the XML-RPC introspection methods in the system
216 namespace.
217
218 see http://xmlrpc.usefulinc.com/doc/reserved.html
219 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000220
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000221 self.funcs.update({'system.listMethods' : self.system_listMethods,
222 'system.methodSignature' : self.system_methodSignature,
223 'system.methodHelp' : self.system_methodHelp})
224
225 def register_multicall_functions(self):
226 """Registers the XML-RPC multicall method in the system
227 namespace.
228
229 see http://www.xmlrpc.com/discuss/msgReader$1208"""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000230
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000231 self.funcs.update({'system.multicall' : self.system_multicall})
Tim Peters2c60f7a2003-01-29 03:49:43 +0000232
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000233 def _marshaled_dispatch(self, data, dispatch_method = None):
234 """Dispatches an XML-RPC method from marshalled (XML) data.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000235
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000236 XML-RPC methods are dispatched from the marshalled (XML) data
237 using the _dispatch method and the result is returned as
238 marshalled data. For backwards compatibility, a dispatch
Tim Peters2c60f7a2003-01-29 03:49:43 +0000239 function can be provided as an argument (see comment in
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000240 SimpleXMLRPCRequestHandler.do_POST) but overriding the
241 existing method through subclassing is the prefered means
242 of changing method dispatch behavior.
243 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000244
Fredrik Lundhb329b712001-09-17 17:35:21 +0000245 try:
Georg Brandl38eceaa2008-05-26 11:14:17 +0000246 params, method = loads(data)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000247
248 # generate response
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000249 if dispatch_method is not None:
250 response = dispatch_method(method, params)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000251 else:
Fredrik Lundhb329b712001-09-17 17:35:21 +0000252 response = self._dispatch(method, params)
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000253 # wrap response in a singleton tuple
254 response = (response,)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000255 response = dumps(response, methodresponse=1,
256 allow_none=self.allow_none, encoding=self.encoding)
Guido van Rossumb940e112007-01-10 16:19:56 +0000257 except Fault as fault:
Georg Brandl38eceaa2008-05-26 11:14:17 +0000258 response = dumps(fault, allow_none=self.allow_none,
259 encoding=self.encoding)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000260 except:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000261 # report exception back to server
Thomas Wouters89f507f2006-12-13 04:49:30 +0000262 exc_type, exc_value, exc_tb = sys.exc_info()
Georg Brandl38eceaa2008-05-26 11:14:17 +0000263 response = dumps(
264 Fault(1, "%s:%s" % (exc_type, exc_value)),
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +0000265 encoding=self.encoding, allow_none=self.allow_none,
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000266 )
267
268 return response
269
270 def system_listMethods(self):
271 """system.listMethods() => ['add', 'subtract', 'multiple']
272
273 Returns a list of the methods supported by the server."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000274
Hye-Shik Chang96042862007-08-19 10:49:11 +0000275 methods = set(self.funcs.keys())
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000276 if self.instance is not None:
277 # Instance can implement _listMethod to return a list of
278 # methods
279 if hasattr(self.instance, '_listMethods'):
Hye-Shik Chang96042862007-08-19 10:49:11 +0000280 methods |= set(self.instance._listMethods())
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000281 # if the instance has a _dispatch method then we
282 # don't have enough information to provide a list
283 # of methods
284 elif not hasattr(self.instance, '_dispatch'):
Hye-Shik Chang96042862007-08-19 10:49:11 +0000285 methods |= set(list_public_methods(self.instance))
286 return sorted(methods)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000287
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000288 def system_methodSignature(self, method_name):
289 """system.methodSignature('add') => [double, int, int]
290
Brett Cannonb9b5f162004-10-03 23:21:44 +0000291 Returns a list describing the signature of the method. In the
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000292 above example, the add method takes two integers as arguments
293 and returns a double result.
294
295 This server does NOT support system.methodSignature."""
296
297 # See http://xmlrpc.usefulinc.com/doc/sysmethodsig.html
Tim Peters2c60f7a2003-01-29 03:49:43 +0000298
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000299 return 'signatures not supported'
300
301 def system_methodHelp(self, method_name):
302 """system.methodHelp('add') => "Adds two integers together"
303
304 Returns a string containing documentation for the specified method."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000305
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000306 method = None
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000307 if method_name in self.funcs:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000308 method = self.funcs[method_name]
309 elif self.instance is not None:
310 # Instance can implement _methodHelp to return help for a method
311 if hasattr(self.instance, '_methodHelp'):
312 return self.instance._methodHelp(method_name)
313 # if the instance has a _dispatch method then we
314 # don't have enough information to provide help
315 elif not hasattr(self.instance, '_dispatch'):
316 try:
317 method = resolve_dotted_attribute(
318 self.instance,
Guido van Rossumd0641422005-02-03 15:01:24 +0000319 method_name,
320 self.allow_dotted_names
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000321 )
322 except AttributeError:
323 pass
324
325 # Note that we aren't checking that the method actually
326 # be a callable object of some kind
327 if method is None:
328 return ""
Fredrik Lundhb329b712001-09-17 17:35:21 +0000329 else:
Neal Norwitz732911f2003-06-29 04:16:28 +0000330 import pydoc
Neal Norwitz3f401f02003-06-29 04:19:37 +0000331 return pydoc.getdoc(method)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000332
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000333 def system_multicall(self, call_list):
334 """system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => \
335[[4], ...]
Fredrik Lundhb329b712001-09-17 17:35:21 +0000336
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000337 Allows the caller to package multiple XML-RPC calls into a single
338 request.
339
Tim Peters2c60f7a2003-01-29 03:49:43 +0000340 See http://www.xmlrpc.com/discuss/msgReader$1208
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000341 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000342
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000343 results = []
344 for call in call_list:
345 method_name = call['methodName']
346 params = call['params']
347
348 try:
349 # XXX A marshalling error in any response will fail the entire
350 # multicall. If someone cares they should fix this.
351 results.append([self._dispatch(method_name, params)])
Guido van Rossumb940e112007-01-10 16:19:56 +0000352 except Fault as fault:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000353 results.append(
354 {'faultCode' : fault.faultCode,
355 'faultString' : fault.faultString}
356 )
357 except:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 exc_type, exc_value, exc_tb = sys.exc_info()
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000359 results.append(
360 {'faultCode' : 1,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000361 'faultString' : "%s:%s" % (exc_type, exc_value)}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000362 )
363 return results
Tim Peters2c60f7a2003-01-29 03:49:43 +0000364
Fredrik Lundhb329b712001-09-17 17:35:21 +0000365 def _dispatch(self, method, params):
366 """Dispatches the XML-RPC method.
367
368 XML-RPC calls are forwarded to a registered function that
369 matches the called XML-RPC method name. If no such function
370 exists then the call is forwarded to the registered instance,
371 if available.
372
373 If the registered instance has a _dispatch method then that
374 method will be called with the name of the XML-RPC method and
Georg Brandl7eb4b7d2005-07-22 21:49:32 +0000375 its parameters as a tuple
Fredrik Lundhb329b712001-09-17 17:35:21 +0000376 e.g. instance._dispatch('add',(2,3))
377
378 If the registered instance does not have a _dispatch method
379 then the instance will be searched to find a matching method
380 and, if found, will be called.
381
382 Methods beginning with an '_' are considered private and will
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000383 not be called.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000384 """
385
Fredrik Lundhb329b712001-09-17 17:35:21 +0000386 func = None
387 try:
388 # check to see if a matching function has been registered
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000389 func = self.funcs[method]
Fredrik Lundhb329b712001-09-17 17:35:21 +0000390 except KeyError:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000391 if self.instance is not None:
Fredrik Lundhb329b712001-09-17 17:35:21 +0000392 # check for a _dispatch method
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000393 if hasattr(self.instance, '_dispatch'):
394 return self.instance._dispatch(method, params)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000395 else:
396 # call instance method directly
397 try:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000398 func = resolve_dotted_attribute(
399 self.instance,
Guido van Rossumd0641422005-02-03 15:01:24 +0000400 method,
401 self.allow_dotted_names
Fredrik Lundhb329b712001-09-17 17:35:21 +0000402 )
403 except AttributeError:
404 pass
405
406 if func is not None:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000407 return func(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000408 else:
409 raise Exception('method "%s" is not supported' % method)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000410
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000411class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
412 """Simple XML-RPC request handler class.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000413
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000414 Handles all HTTP POST requests and attempts to decode them as
415 XML-RPC requests.
416 """
417
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000418 # Class attribute listing the accessible path components;
419 # paths not on this list will result in a 404 error.
420 rpc_paths = ('/', '/RPC2')
421
422 def is_rpc_path_valid(self):
423 if self.rpc_paths:
424 return self.path in self.rpc_paths
425 else:
426 # If .rpc_paths is empty, just assume all paths are legal
427 return True
428
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000429 def do_POST(self):
430 """Handles the HTTP POST request.
431
432 Attempts to interpret all HTTP POST requests as XML-RPC calls,
433 which are forwarded to the server's _dispatch method for handling.
434 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000435
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436 # Check that the path is legal
437 if not self.is_rpc_path_valid():
438 self.report_404()
439 return
440
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000441 try:
Tim Peters536cf992005-12-25 23:18:31 +0000442 # Get arguments by reading body of request.
443 # We read this in chunks to avoid straining
Andrew M. Kuchlinge63fde72005-12-04 15:36:57 +0000444 # socket.read(); around the 10 or 15Mb mark, some platforms
445 # begin to have problems (bug #792570).
446 max_chunk_size = 10*1024*1024
447 size_remaining = int(self.headers["content-length"])
448 L = []
449 while size_remaining:
450 chunk_size = min(size_remaining, max_chunk_size)
451 L.append(self.rfile.read(chunk_size))
452 size_remaining -= len(L[-1])
Hye-Shik Chang96042862007-08-19 10:49:11 +0000453 data = b''.join(L)
Andrew M. Kuchlinge63fde72005-12-04 15:36:57 +0000454
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000455 # In previous versions of SimpleXMLRPCServer, _dispatch
456 # could be overridden in this class, instead of in
457 # SimpleXMLRPCDispatcher. To maintain backwards compatibility,
458 # check to see if a subclass implements _dispatch and dispatch
459 # using that method if present.
460 response = self.server._marshaled_dispatch(
461 data, getattr(self, '_dispatch', None)
462 )
Guido van Rossum61e21b52007-08-20 19:06:03 +0000463 except Exception as e: # This should only happen if the module is buggy
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000464 # internal error, report as HTTP server error
465 self.send_response(500)
Guido van Rossum61e21b52007-08-20 19:06:03 +0000466
467 # Send information about the exception if requested
468 if hasattr(self.server, '_send_traceback_header') and \
469 self.server._send_traceback_header:
470 self.send_header("X-exception", str(e))
471 self.send_header("X-traceback", traceback.format_exc())
472
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000473 self.end_headers()
474 else:
Guido van Rossum8a392d72007-11-21 22:09:45 +0000475 # Got a valid XML RPC response; convert to bytes first
476 response = response.encode("utf-8")
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000477 self.send_response(200)
478 self.send_header("Content-type", "text/xml")
479 self.send_header("Content-length", str(len(response)))
480 self.end_headers()
481 self.wfile.write(response)
482
483 # shut down the connection
484 self.wfile.flush()
485 self.connection.shutdown(1)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000486
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000487 def report_404 (self):
488 # Report a 404 error
489 self.send_response(404)
Christian Heimes0aa93cd2007-12-08 18:38:20 +0000490 response = b'No such page'
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000491 self.send_header("Content-type", "text/plain")
492 self.send_header("Content-length", str(len(response)))
493 self.end_headers()
494 self.wfile.write(response)
495 # shut down the connection
496 self.wfile.flush()
497 self.connection.shutdown(1)
498
Fredrik Lundhb329b712001-09-17 17:35:21 +0000499 def log_request(self, code='-', size='-'):
500 """Selectively log an accepted request."""
501
502 if self.server.logRequests:
503 BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
504
Alexandre Vassalottice261952008-05-12 02:31:37 +0000505class SimpleXMLRPCServer(socketserver.TCPServer,
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000506 SimpleXMLRPCDispatcher):
Fredrik Lundhb329b712001-09-17 17:35:21 +0000507 """Simple XML-RPC server.
508
509 Simple XML-RPC server that allows functions and a single instance
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000510 to be installed to handle requests. The default implementation
511 attempts to dispatch XML-RPC calls to the functions or instance
512 installed in the server. Override the _dispatch method inhereted
513 from SimpleXMLRPCDispatcher to change this behavior.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000514 """
515
Andrew M. Kuchling3a976052005-12-04 15:07:41 +0000516 allow_reuse_address = True
517
Guido van Rossum61e21b52007-08-20 19:06:03 +0000518 # Warning: this is for debugging purposes only! Never set this to True in
519 # production code, as will be sending out sensitive information (exception
520 # and stack trace details) when exceptions are raised inside
521 # SimpleXMLRPCRequestHandler.do_POST
522 _send_traceback_header = False
523
Fredrik Lundhb329b712001-09-17 17:35:21 +0000524 def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000525 logRequests=True, allow_none=False, encoding=None, bind_and_activate=True):
Fredrik Lundhb329b712001-09-17 17:35:21 +0000526 self.logRequests = logRequests
Tim Peters2c60f7a2003-01-29 03:49:43 +0000527
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +0000528 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
Alexandre Vassalottice261952008-05-12 02:31:37 +0000529 socketserver.TCPServer.__init__(self, addr, requestHandler, bind_and_activate)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000530
Tim Peters536cf992005-12-25 23:18:31 +0000531 # [Bug #1222790] If possible, set close-on-exec flag; if a
532 # method spawns a subprocess, the subprocess shouldn't have
Andrew M. Kuchling3a976052005-12-04 15:07:41 +0000533 # the listening socket open.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'):
Andrew M. Kuchling3a976052005-12-04 15:07:41 +0000535 flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
536 flags |= fcntl.FD_CLOEXEC
537 fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)
538
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000539class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher):
540 """Simple handler for XML-RPC data passed through CGI."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000541
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +0000542 def __init__(self, allow_none=False, encoding=None):
543 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000544
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000545 def handle_xmlrpc(self, request_text):
546 """Handle a single XML-RPC request"""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000547
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000548 response = self._marshaled_dispatch(request_text)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000549
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000550 print('Content-Type: text/xml')
551 print('Content-Length: %d' % len(response))
552 print()
Martin v. Löwis9c5ea502003-05-01 05:05:09 +0000553 sys.stdout.write(response)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000554
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000555 def handle_get(self):
556 """Handle a single HTTP GET request.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000557
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000558 Default implementation indicates an error because
559 XML-RPC uses the POST method.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000560 """
561
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000562 code = 400
563 message, explain = \
564 BaseHTTPServer.BaseHTTPRequestHandler.responses[code]
Tim Peters2c60f7a2003-01-29 03:49:43 +0000565
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000566 response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \
567 {
Tim Peters2c60f7a2003-01-29 03:49:43 +0000568 'code' : code,
569 'message' : message,
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000570 'explain' : explain
571 }
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000572 print('Status: %d %s' % (code, message))
573 print('Content-Type: text/html')
574 print('Content-Length: %d' % len(response))
575 print()
Neal Norwitz732911f2003-06-29 04:16:28 +0000576 sys.stdout.write(response)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000577
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000578 def handle_request(self, request_text = None):
579 """Handle a single XML-RPC request passed through a CGI post method.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000580
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000581 If no XML data is given then it is read from stdin. The resulting
582 XML-RPC response is printed to stdout along with the correct HTTP
583 headers.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000584 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000585
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000586 if request_text is None and \
587 os.environ.get('REQUEST_METHOD', None) == 'GET':
588 self.handle_get()
589 else:
590 # POST data is normally available through stdin
591 if request_text is None:
Tim Peters2c60f7a2003-01-29 03:49:43 +0000592 request_text = sys.stdin.read()
Fredrik Lundhb329b712001-09-17 17:35:21 +0000593
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000594 self.handle_xmlrpc(request_text)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000595
Georg Brandl38eceaa2008-05-26 11:14:17 +0000596
597# -----------------------------------------------------------------------------
598# Self documenting XML-RPC Server.
599
600class ServerHTMLDoc(pydoc.HTMLDoc):
601 """Class used to generate pydoc HTML document for a server"""
602
603 def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
604 """Mark up some plain text, given a context of symbols to look for.
605 Each context dictionary maps object names to anchor names."""
606 escape = escape or self.escape
607 results = []
608 here = 0
609
610 # XXX Note that this regular expression does not allow for the
611 # hyperlinking of arbitrary strings being used as method
612 # names. Only methods with names consisting of word characters
613 # and '.'s are hyperlinked.
614 pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|'
615 r'RFC[- ]?(\d+)|'
616 r'PEP[- ]?(\d+)|'
617 r'(self\.)?((?:\w|\.)+))\b')
618 while 1:
619 match = pattern.search(text, here)
620 if not match: break
621 start, end = match.span()
622 results.append(escape(text[here:start]))
623
624 all, scheme, rfc, pep, selfdot, name = match.groups()
625 if scheme:
626 url = escape(all).replace('"', '"')
627 results.append('<a href="%s">%s</a>' % (url, url))
628 elif rfc:
629 url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc)
630 results.append('<a href="%s">%s</a>' % (url, escape(all)))
631 elif pep:
632 url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep)
633 results.append('<a href="%s">%s</a>' % (url, escape(all)))
634 elif text[end:end+1] == '(':
635 results.append(self.namelink(name, methods, funcs, classes))
636 elif selfdot:
637 results.append('self.<strong>%s</strong>' % name)
638 else:
639 results.append(self.namelink(name, classes))
640 here = end
641 results.append(escape(text[here:]))
642 return ''.join(results)
643
644 def docroutine(self, object, name, mod=None,
645 funcs={}, classes={}, methods={}, cl=None):
646 """Produce HTML documentation for a function or method object."""
647
648 anchor = (cl and cl.__name__ or '') + '-' + name
649 note = ''
650
651 title = '<a name="%s"><strong>%s</strong></a>' % (
652 self.escape(anchor), self.escape(name))
653
654 if inspect.ismethod(object):
655 args, varargs, varkw, defaults = inspect.getargspec(object)
656 # exclude the argument bound to the instance, it will be
657 # confusing to the non-Python user
658 argspec = inspect.formatargspec (
659 args[1:],
660 varargs,
661 varkw,
662 defaults,
663 formatvalue=self.formatvalue
664 )
665 elif inspect.isfunction(object):
666 args, varargs, varkw, defaults = inspect.getargspec(object)
667 argspec = inspect.formatargspec(
668 args, varargs, varkw, defaults, formatvalue=self.formatvalue)
669 else:
670 argspec = '(...)'
671
672 if isinstance(object, tuple):
673 argspec = object[0] or argspec
674 docstring = object[1] or ""
675 else:
676 docstring = pydoc.getdoc(object)
677
678 decl = title + argspec + (note and self.grey(
679 '<font face="helvetica, arial">%s</font>' % note))
680
681 doc = self.markup(
682 docstring, self.preformat, funcs, classes, methods)
683 doc = doc and '<dd><tt>%s</tt></dd>' % doc
684 return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc)
685
686 def docserver(self, server_name, package_documentation, methods):
687 """Produce HTML documentation for an XML-RPC server."""
688
689 fdict = {}
690 for key, value in methods.items():
691 fdict[key] = '#-' + key
692 fdict[value] = fdict[key]
693
694 server_name = self.escape(server_name)
695 head = '<big><big><strong>%s</strong></big></big>' % server_name
696 result = self.heading(head, '#ffffff', '#7799ee')
697
698 doc = self.markup(package_documentation, self.preformat, fdict)
699 doc = doc and '<tt>%s</tt>' % doc
700 result = result + '<p>%s</p>\n' % doc
701
702 contents = []
703 method_items = sorted(methods.items())
704 for key, value in method_items:
705 contents.append(self.docroutine(value, key, funcs=fdict))
706 result = result + self.bigsection(
707 'Methods', '#ffffff', '#eeaa77', ''.join(contents))
708
709 return result
710
711class XMLRPCDocGenerator:
712 """Generates documentation for an XML-RPC server.
713
714 This class is designed as mix-in and should not
715 be constructed directly.
716 """
717
718 def __init__(self):
719 # setup variables used for HTML documentation
720 self.server_name = 'XML-RPC Server Documentation'
721 self.server_documentation = \
722 "This server exports the following methods through the XML-RPC "\
723 "protocol."
724 self.server_title = 'XML-RPC Server Documentation'
725
726 def set_server_title(self, server_title):
727 """Set the HTML title of the generated server documentation"""
728
729 self.server_title = server_title
730
731 def set_server_name(self, server_name):
732 """Set the name of the generated HTML server documentation"""
733
734 self.server_name = server_name
735
736 def set_server_documentation(self, server_documentation):
737 """Set the documentation string for the entire server."""
738
739 self.server_documentation = server_documentation
740
741 def generate_html_documentation(self):
742 """generate_html_documentation() => html documentation for the server
743
744 Generates HTML documentation for the server using introspection for
745 installed functions and instances that do not implement the
746 _dispatch method. Alternatively, instances can choose to implement
747 the _get_method_argstring(method_name) method to provide the
748 argument string used in the documentation and the
749 _methodHelp(method_name) method to provide the help text used
750 in the documentation."""
751
752 methods = {}
753
754 for method_name in self.system_listMethods():
755 if method_name in self.funcs:
756 method = self.funcs[method_name]
757 elif self.instance is not None:
758 method_info = [None, None] # argspec, documentation
759 if hasattr(self.instance, '_get_method_argstring'):
760 method_info[0] = self.instance._get_method_argstring(method_name)
761 if hasattr(self.instance, '_methodHelp'):
762 method_info[1] = self.instance._methodHelp(method_name)
763
764 method_info = tuple(method_info)
765 if method_info != (None, None):
766 method = method_info
767 elif not hasattr(self.instance, '_dispatch'):
768 try:
769 method = resolve_dotted_attribute(
770 self.instance,
771 method_name
772 )
773 except AttributeError:
774 method = method_info
775 else:
776 method = method_info
777 else:
778 assert 0, "Could not find method in self.functions and no "\
779 "instance installed"
780
781 methods[method_name] = method
782
783 documenter = ServerHTMLDoc()
784 documentation = documenter.docserver(
785 self.server_name,
786 self.server_documentation,
787 methods
788 )
789
790 return documenter.page(self.server_title, documentation)
791
792class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
793 """XML-RPC and documentation request handler class.
794
795 Handles all HTTP POST requests and attempts to decode them as
796 XML-RPC requests.
797
798 Handles all HTTP GET requests and interprets them as requests
799 for documentation.
800 """
801
802 def do_GET(self):
803 """Handles the HTTP GET request.
804
805 Interpret all HTTP GET requests as requests for server
806 documentation.
807 """
808 # Check that the path is legal
809 if not self.is_rpc_path_valid():
810 self.report_404()
811 return
812
813 response = self.server.generate_html_documentation()
814 self.send_response(200)
815 self.send_header("Content-type", "text/html")
816 self.send_header("Content-length", str(len(response)))
817 self.end_headers()
818 self.wfile.write(response.encode())
819
820 # shut down the connection
821 self.wfile.flush()
822 self.connection.shutdown(1)
823
824class DocXMLRPCServer( SimpleXMLRPCServer,
825 XMLRPCDocGenerator):
826 """XML-RPC and HTML documentation server.
827
828 Adds the ability to serve server documentation to the capabilities
829 of SimpleXMLRPCServer.
830 """
831
832 def __init__(self, addr, requestHandler=DocXMLRPCRequestHandler,
833 logRequests=1, allow_none=False, encoding=None,
834 bind_and_activate=True):
835 SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests,
836 allow_none, encoding, bind_and_activate)
837 XMLRPCDocGenerator.__init__(self)
838
839class DocCGIXMLRPCRequestHandler( CGIXMLRPCRequestHandler,
840 XMLRPCDocGenerator):
841 """Handler for XML-RPC data and documentation requests passed through
842 CGI"""
843
844 def handle_get(self):
845 """Handles the HTTP GET request.
846
847 Interpret all HTTP GET requests as requests for server
848 documentation.
849 """
850
851 response = self.generate_html_documentation()
852
853 print('Content-Type: text/html')
854 print('Content-Length: %d' % len(response))
855 print()
856 sys.stdout.write(response)
857
858 def __init__(self):
859 CGIXMLRPCRequestHandler.__init__(self)
860 XMLRPCDocGenerator.__init__(self)
861
862
Fredrik Lundhb329b712001-09-17 17:35:21 +0000863if __name__ == '__main__':
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000864 print('Running XML-RPC server on port 8000')
Fredrik Lundhb329b712001-09-17 17:35:21 +0000865 server = SimpleXMLRPCServer(("localhost", 8000))
866 server.register_function(pow)
867 server.register_function(lambda x,y: x+y, 'add')
868 server.serve_forever()