Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 1 | """Simple XML-RPC Server. |
| 2 | |
| 3 | This module can be used to create simple XML-RPC servers |
| 4 | by creating a server and either installing functions, a |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 5 | class instance, or by extending the SimpleXMLRPCServer |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 6 | class. |
| 7 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 8 | It can also be used to handle XML-RPC requests in a CGI |
| 9 | environment using CGIXMLRPCRequestHandler. |
| 10 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 11 | A list of possible usage patterns follows: |
| 12 | |
| 13 | 1. Install functions: |
| 14 | |
| 15 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 16 | server.register_function(pow) |
| 17 | server.register_function(lambda x,y: x+y, 'add') |
| 18 | server.serve_forever() |
| 19 | |
| 20 | 2. Install an instance: |
| 21 | |
| 22 | class MyFuncs: |
| 23 | def __init__(self): |
| 24 | # make all of the string functions available through |
| 25 | # string.func_name |
| 26 | import string |
| 27 | self.string = string |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 28 | def _listMethods(self): |
| 29 | # implement this method so that system.listMethods |
| 30 | # knows to advertise the strings methods |
| 31 | return list_public_methods(self) + \ |
| 32 | ['string.' + method for method in list_public_methods(self.string)] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 33 | def pow(self, x, y): return pow(x, y) |
| 34 | def add(self, x, y) : return x + y |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 35 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 36 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 37 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 38 | server.register_instance(MyFuncs()) |
| 39 | server.serve_forever() |
| 40 | |
| 41 | 3. Install an instance with custom dispatch method: |
| 42 | |
| 43 | class Math: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 44 | def _listMethods(self): |
| 45 | # this method must be present for system.listMethods |
| 46 | # to work |
| 47 | return ['add', 'pow'] |
| 48 | def _methodHelp(self, method): |
| 49 | # this method must be present for system.methodHelp |
| 50 | # to work |
| 51 | if method == 'add': |
| 52 | return "add(2,3) => 5" |
| 53 | elif method == 'pow': |
| 54 | return "pow(x, y[, z]) => number" |
| 55 | else: |
| 56 | # By convention, return empty |
| 57 | # string if no help is available |
| 58 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 59 | def _dispatch(self, method, params): |
| 60 | if method == 'pow': |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 61 | return pow(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 62 | elif method == 'add': |
| 63 | return params[0] + params[1] |
| 64 | else: |
| 65 | raise 'bad method' |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 66 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 67 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 68 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 69 | server.register_instance(Math()) |
| 70 | server.serve_forever() |
| 71 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 72 | 4. Subclass SimpleXMLRPCServer: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 73 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 74 | class MathServer(SimpleXMLRPCServer): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 75 | def _dispatch(self, method, params): |
| 76 | try: |
| 77 | # We are forcing the 'export_' prefix on methods that are |
| 78 | # callable through XML-RPC to prevent potential security |
| 79 | # problems |
| 80 | func = getattr(self, 'export_' + method) |
| 81 | except AttributeError: |
| 82 | raise Exception('method "%s" is not supported' % method) |
| 83 | else: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 84 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 85 | |
| 86 | def export_add(self, x, y): |
| 87 | return x + y |
| 88 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 89 | server = MathServer(("localhost", 8000)) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 90 | server.serve_forever() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 91 | |
| 92 | 5. CGI script: |
| 93 | |
| 94 | server = CGIXMLRPCRequestHandler() |
| 95 | server.register_function(pow) |
| 96 | server.handle_request() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 97 | """ |
| 98 | |
| 99 | # Written by Brian Quinlan (brian@sweetapp.com). |
| 100 | # Based on code written by Fredrik Lundh. |
| 101 | |
| 102 | import xmlrpclib |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 103 | from xmlrpclib import Fault |
Georg Brandl | e152a77 | 2008-05-24 18:31:28 +0000 | [diff] [blame] | 104 | import SocketServer |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 105 | import BaseHTTPServer |
| 106 | import sys |
Anthony Baxter | e29002c | 2006-04-12 12:07:31 +0000 | [diff] [blame] | 107 | import os |
Facundo Batista | 7f686fc | 2007-08-17 19:16:44 +0000 | [diff] [blame] | 108 | import traceback |
Anthony Baxter | e29002c | 2006-04-12 12:07:31 +0000 | [diff] [blame] | 109 | try: |
| 110 | import fcntl |
| 111 | except ImportError: |
| 112 | fcntl = None |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 114 | def resolve_dotted_attribute(obj, attr, allow_dotted_names=True): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 115 | """resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 116 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 117 | Resolves a dotted attribute name to an object. Raises |
| 118 | an AttributeError if any attribute in the chain starts with a '_'. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 119 | |
| 120 | If the optional allow_dotted_names argument is false, dots are not |
| 121 | supported and this function operates similar to getattr(obj, attr). |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 122 | """ |
| 123 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 124 | if allow_dotted_names: |
| 125 | attrs = attr.split('.') |
| 126 | else: |
| 127 | attrs = [attr] |
| 128 | |
| 129 | for i in attrs: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 130 | if i.startswith('_'): |
| 131 | raise AttributeError( |
| 132 | 'attempt to access private attribute "%s"' % i |
| 133 | ) |
| 134 | else: |
| 135 | obj = getattr(obj,i) |
| 136 | return obj |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 137 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 138 | def list_public_methods(obj): |
| 139 | """Returns a list of attribute strings, found in the specified |
| 140 | object, which represent callable attributes""" |
| 141 | |
| 142 | return [member for member in dir(obj) |
| 143 | if not member.startswith('_') and |
| 144 | callable(getattr(obj, member))] |
| 145 | |
| 146 | def remove_duplicates(lst): |
| 147 | """remove_duplicates([2,2,2,1,3,3]) => [3,1,2] |
| 148 | |
| 149 | Returns a copy of a list without duplicates. Every list |
| 150 | item must be hashable and the order of the items in the |
| 151 | resulting list is not defined. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 152 | """ |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 153 | u = {} |
| 154 | for x in lst: |
| 155 | u[x] = 1 |
| 156 | |
| 157 | return u.keys() |
| 158 | |
| 159 | class SimpleXMLRPCDispatcher: |
| 160 | """Mix-in class that dispatches XML-RPC requests. |
| 161 | |
| 162 | This class is used to register XML-RPC method handlers |
| 163 | and then to dispatch them. There should never be any |
| 164 | reason to instantiate this class directly. |
| 165 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 166 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 167 | def __init__(self, allow_none, encoding): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 168 | self.funcs = {} |
| 169 | self.instance = None |
Andrew M. Kuchling | 10a16de | 2005-12-04 16:34:40 +0000 | [diff] [blame] | 170 | self.allow_none = allow_none |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 171 | self.encoding = encoding |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 173 | def register_instance(self, instance, allow_dotted_names=False): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 174 | """Registers an instance to respond to XML-RPC requests. |
| 175 | |
| 176 | Only one instance can be installed at a time. |
| 177 | |
| 178 | If the registered instance has a _dispatch method then that |
| 179 | method will be called with the name of the XML-RPC method and |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 180 | its parameters as a tuple |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 181 | e.g. instance._dispatch('add',(2,3)) |
| 182 | |
| 183 | If the registered instance does not have a _dispatch method |
| 184 | then the instance will be searched to find a matching method |
| 185 | and, if found, will be called. Methods beginning with an '_' |
| 186 | are considered private and will not be called by |
| 187 | SimpleXMLRPCServer. |
| 188 | |
| 189 | If a registered function matches a XML-RPC request, then it |
| 190 | will be called instead of the registered instance. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 191 | |
| 192 | If the optional allow_dotted_names argument is true and the |
| 193 | instance does not have a _dispatch method, method names |
| 194 | containing dots are supported and resolved, as long as none of |
| 195 | the name segments start with an '_'. |
| 196 | |
| 197 | *** SECURITY WARNING: *** |
| 198 | |
| 199 | Enabling the allow_dotted_names options allows intruders |
| 200 | to access your module's global variables and may allow |
| 201 | intruders to execute arbitrary code on your machine. Only |
| 202 | use this option on a secure, closed network. |
| 203 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 204 | """ |
| 205 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 206 | self.instance = instance |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 207 | self.allow_dotted_names = allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 208 | |
| 209 | def register_function(self, function, name = None): |
| 210 | """Registers a function to respond to XML-RPC requests. |
| 211 | |
| 212 | The optional name argument can be used to set a Unicode name |
| 213 | for the function. |
| 214 | """ |
| 215 | |
| 216 | if name is None: |
| 217 | name = function.__name__ |
| 218 | self.funcs[name] = function |
| 219 | |
| 220 | def register_introspection_functions(self): |
| 221 | """Registers the XML-RPC introspection methods in the system |
| 222 | namespace. |
| 223 | |
| 224 | see http://xmlrpc.usefulinc.com/doc/reserved.html |
| 225 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 226 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 227 | self.funcs.update({'system.listMethods' : self.system_listMethods, |
| 228 | 'system.methodSignature' : self.system_methodSignature, |
| 229 | 'system.methodHelp' : self.system_methodHelp}) |
| 230 | |
| 231 | def register_multicall_functions(self): |
| 232 | """Registers the XML-RPC multicall method in the system |
| 233 | namespace. |
| 234 | |
| 235 | see http://www.xmlrpc.com/discuss/msgReader$1208""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 236 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 237 | self.funcs.update({'system.multicall' : self.system_multicall}) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 238 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 239 | def _marshaled_dispatch(self, data, dispatch_method = None): |
| 240 | """Dispatches an XML-RPC method from marshalled (XML) data. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 241 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 242 | XML-RPC methods are dispatched from the marshalled (XML) data |
| 243 | using the _dispatch method and the result is returned as |
| 244 | marshalled data. For backwards compatibility, a dispatch |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 245 | function can be provided as an argument (see comment in |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 246 | SimpleXMLRPCRequestHandler.do_POST) but overriding the |
| 247 | existing method through subclassing is the prefered means |
| 248 | of changing method dispatch behavior. |
| 249 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 250 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 251 | try: |
Georg Brandl | b9120e7 | 2006-06-01 12:30:46 +0000 | [diff] [blame] | 252 | params, method = xmlrpclib.loads(data) |
| 253 | |
| 254 | # generate response |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 255 | if dispatch_method is not None: |
| 256 | response = dispatch_method(method, params) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 257 | else: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 258 | response = self._dispatch(method, params) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 259 | # wrap response in a singleton tuple |
| 260 | response = (response,) |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 261 | response = xmlrpclib.dumps(response, methodresponse=1, |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 262 | allow_none=self.allow_none, encoding=self.encoding) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 263 | except Fault, fault: |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 264 | response = xmlrpclib.dumps(fault, allow_none=self.allow_none, |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 265 | encoding=self.encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 266 | except: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 267 | # report exception back to server |
Andrew M. Kuchling | a5453c4 | 2006-09-05 13:15:41 +0000 | [diff] [blame] | 268 | exc_type, exc_value, exc_tb = sys.exc_info() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 269 | response = xmlrpclib.dumps( |
Andrew M. Kuchling | a5453c4 | 2006-09-05 13:15:41 +0000 | [diff] [blame] | 270 | xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)), |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 271 | encoding=self.encoding, allow_none=self.allow_none, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 272 | ) |
| 273 | |
| 274 | return response |
| 275 | |
| 276 | def system_listMethods(self): |
| 277 | """system.listMethods() => ['add', 'subtract', 'multiple'] |
| 278 | |
| 279 | Returns a list of the methods supported by the server.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 280 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 281 | methods = self.funcs.keys() |
| 282 | if self.instance is not None: |
| 283 | # Instance can implement _listMethod to return a list of |
| 284 | # methods |
| 285 | if hasattr(self.instance, '_listMethods'): |
| 286 | methods = remove_duplicates( |
| 287 | methods + self.instance._listMethods() |
| 288 | ) |
| 289 | # if the instance has a _dispatch method then we |
| 290 | # don't have enough information to provide a list |
| 291 | # of methods |
| 292 | elif not hasattr(self.instance, '_dispatch'): |
| 293 | methods = remove_duplicates( |
| 294 | methods + list_public_methods(self.instance) |
| 295 | ) |
| 296 | methods.sort() |
| 297 | return methods |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 298 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 299 | def system_methodSignature(self, method_name): |
| 300 | """system.methodSignature('add') => [double, int, int] |
| 301 | |
Brett Cannon | b9b5f16 | 2004-10-03 23:21:44 +0000 | [diff] [blame] | 302 | Returns a list describing the signature of the method. In the |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 303 | above example, the add method takes two integers as arguments |
| 304 | and returns a double result. |
| 305 | |
| 306 | This server does NOT support system.methodSignature.""" |
| 307 | |
| 308 | # See http://xmlrpc.usefulinc.com/doc/sysmethodsig.html |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 309 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 310 | return 'signatures not supported' |
| 311 | |
| 312 | def system_methodHelp(self, method_name): |
| 313 | """system.methodHelp('add') => "Adds two integers together" |
| 314 | |
| 315 | Returns a string containing documentation for the specified method.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 316 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 317 | method = None |
| 318 | if self.funcs.has_key(method_name): |
| 319 | method = self.funcs[method_name] |
| 320 | elif self.instance is not None: |
| 321 | # Instance can implement _methodHelp to return help for a method |
| 322 | if hasattr(self.instance, '_methodHelp'): |
| 323 | return self.instance._methodHelp(method_name) |
| 324 | # if the instance has a _dispatch method then we |
| 325 | # don't have enough information to provide help |
| 326 | elif not hasattr(self.instance, '_dispatch'): |
| 327 | try: |
| 328 | method = resolve_dotted_attribute( |
| 329 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 330 | method_name, |
| 331 | self.allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 332 | ) |
| 333 | except AttributeError: |
| 334 | pass |
| 335 | |
| 336 | # Note that we aren't checking that the method actually |
| 337 | # be a callable object of some kind |
| 338 | if method is None: |
| 339 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 340 | else: |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 341 | import pydoc |
Neal Norwitz | 3f401f0 | 2003-06-29 04:19:37 +0000 | [diff] [blame] | 342 | return pydoc.getdoc(method) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 343 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 344 | def system_multicall(self, call_list): |
| 345 | """system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => \ |
| 346 | [[4], ...] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 348 | Allows the caller to package multiple XML-RPC calls into a single |
| 349 | request. |
| 350 | |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 351 | See http://www.xmlrpc.com/discuss/msgReader$1208 |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 352 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 353 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 354 | results = [] |
| 355 | for call in call_list: |
| 356 | method_name = call['methodName'] |
| 357 | params = call['params'] |
| 358 | |
| 359 | try: |
| 360 | # XXX A marshalling error in any response will fail the entire |
| 361 | # multicall. If someone cares they should fix this. |
| 362 | results.append([self._dispatch(method_name, params)]) |
| 363 | except Fault, fault: |
| 364 | results.append( |
| 365 | {'faultCode' : fault.faultCode, |
| 366 | 'faultString' : fault.faultString} |
| 367 | ) |
| 368 | except: |
Andrew M. Kuchling | a5453c4 | 2006-09-05 13:15:41 +0000 | [diff] [blame] | 369 | exc_type, exc_value, exc_tb = sys.exc_info() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 370 | results.append( |
| 371 | {'faultCode' : 1, |
Andrew M. Kuchling | a5453c4 | 2006-09-05 13:15:41 +0000 | [diff] [blame] | 372 | 'faultString' : "%s:%s" % (exc_type, exc_value)} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 373 | ) |
| 374 | return results |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 375 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 376 | def _dispatch(self, method, params): |
| 377 | """Dispatches the XML-RPC method. |
| 378 | |
| 379 | XML-RPC calls are forwarded to a registered function that |
| 380 | matches the called XML-RPC method name. If no such function |
| 381 | exists then the call is forwarded to the registered instance, |
| 382 | if available. |
| 383 | |
| 384 | If the registered instance has a _dispatch method then that |
| 385 | method will be called with the name of the XML-RPC method and |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 386 | its parameters as a tuple |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 387 | e.g. instance._dispatch('add',(2,3)) |
| 388 | |
| 389 | If the registered instance does not have a _dispatch method |
| 390 | then the instance will be searched to find a matching method |
| 391 | and, if found, will be called. |
| 392 | |
| 393 | Methods beginning with an '_' are considered private and will |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 394 | not be called. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 395 | """ |
| 396 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 397 | func = None |
| 398 | try: |
| 399 | # check to see if a matching function has been registered |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 400 | func = self.funcs[method] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 401 | except KeyError: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 402 | if self.instance is not None: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 403 | # check for a _dispatch method |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 404 | if hasattr(self.instance, '_dispatch'): |
| 405 | return self.instance._dispatch(method, params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 406 | else: |
| 407 | # call instance method directly |
| 408 | try: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 409 | func = resolve_dotted_attribute( |
| 410 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 411 | method, |
| 412 | self.allow_dotted_names |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 413 | ) |
| 414 | except AttributeError: |
| 415 | pass |
| 416 | |
| 417 | if func is not None: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 418 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 419 | else: |
| 420 | raise Exception('method "%s" is not supported' % method) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 421 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 422 | class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 423 | """Simple XML-RPC request handler class. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 424 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 425 | Handles all HTTP POST requests and attempts to decode them as |
| 426 | XML-RPC requests. |
| 427 | """ |
| 428 | |
Andrew M. Kuchling | 622f144 | 2006-05-31 14:08:48 +0000 | [diff] [blame] | 429 | # Class attribute listing the accessible path components; |
| 430 | # paths not on this list will result in a 404 error. |
| 431 | rpc_paths = ('/', '/RPC2') |
| 432 | |
| 433 | def is_rpc_path_valid(self): |
| 434 | if self.rpc_paths: |
| 435 | return self.path in self.rpc_paths |
| 436 | else: |
| 437 | # If .rpc_paths is empty, just assume all paths are legal |
| 438 | return True |
| 439 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 440 | def do_POST(self): |
| 441 | """Handles the HTTP POST request. |
| 442 | |
| 443 | Attempts to interpret all HTTP POST requests as XML-RPC calls, |
| 444 | which are forwarded to the server's _dispatch method for handling. |
| 445 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 446 | |
Andrew M. Kuchling | 622f144 | 2006-05-31 14:08:48 +0000 | [diff] [blame] | 447 | # Check that the path is legal |
| 448 | if not self.is_rpc_path_valid(): |
| 449 | self.report_404() |
| 450 | return |
Tim Peters | 5535da0 | 2006-06-01 13:41:46 +0000 | [diff] [blame] | 451 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 452 | try: |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 453 | # Get arguments by reading body of request. |
| 454 | # We read this in chunks to avoid straining |
Andrew M. Kuchling | e63fde7 | 2005-12-04 15:36:57 +0000 | [diff] [blame] | 455 | # socket.read(); around the 10 or 15Mb mark, some platforms |
| 456 | # begin to have problems (bug #792570). |
| 457 | max_chunk_size = 10*1024*1024 |
| 458 | size_remaining = int(self.headers["content-length"]) |
| 459 | L = [] |
| 460 | while size_remaining: |
| 461 | chunk_size = min(size_remaining, max_chunk_size) |
| 462 | L.append(self.rfile.read(chunk_size)) |
| 463 | size_remaining -= len(L[-1]) |
| 464 | data = ''.join(L) |
| 465 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 466 | # In previous versions of SimpleXMLRPCServer, _dispatch |
| 467 | # could be overridden in this class, instead of in |
| 468 | # SimpleXMLRPCDispatcher. To maintain backwards compatibility, |
| 469 | # check to see if a subclass implements _dispatch and dispatch |
| 470 | # using that method if present. |
| 471 | response = self.server._marshaled_dispatch( |
| 472 | data, getattr(self, '_dispatch', None) |
| 473 | ) |
Facundo Batista | 7f686fc | 2007-08-17 19:16:44 +0000 | [diff] [blame] | 474 | except Exception, e: # This should only happen if the module is buggy |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 475 | # internal error, report as HTTP server error |
| 476 | self.send_response(500) |
Facundo Batista | 7f686fc | 2007-08-17 19:16:44 +0000 | [diff] [blame] | 477 | |
| 478 | # Send information about the exception if requested |
| 479 | if hasattr(self.server, '_send_traceback_header') and \ |
| 480 | self.server._send_traceback_header: |
| 481 | self.send_header("X-exception", str(e)) |
| 482 | self.send_header("X-traceback", traceback.format_exc()) |
| 483 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 484 | self.end_headers() |
| 485 | else: |
| 486 | # got a valid XML RPC response |
| 487 | self.send_response(200) |
| 488 | self.send_header("Content-type", "text/xml") |
| 489 | self.send_header("Content-length", str(len(response))) |
| 490 | self.end_headers() |
| 491 | self.wfile.write(response) |
| 492 | |
| 493 | # shut down the connection |
| 494 | self.wfile.flush() |
| 495 | self.connection.shutdown(1) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 496 | |
Andrew M. Kuchling | 622f144 | 2006-05-31 14:08:48 +0000 | [diff] [blame] | 497 | def report_404 (self): |
| 498 | # Report a 404 error |
Tim Peters | 5535da0 | 2006-06-01 13:41:46 +0000 | [diff] [blame] | 499 | self.send_response(404) |
| 500 | response = 'No such page' |
| 501 | self.send_header("Content-type", "text/plain") |
| 502 | self.send_header("Content-length", str(len(response))) |
| 503 | self.end_headers() |
| 504 | self.wfile.write(response) |
| 505 | # shut down the connection |
| 506 | self.wfile.flush() |
| 507 | self.connection.shutdown(1) |
Andrew M. Kuchling | 622f144 | 2006-05-31 14:08:48 +0000 | [diff] [blame] | 508 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 509 | def log_request(self, code='-', size='-'): |
| 510 | """Selectively log an accepted request.""" |
| 511 | |
| 512 | if self.server.logRequests: |
| 513 | BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size) |
| 514 | |
Georg Brandl | e152a77 | 2008-05-24 18:31:28 +0000 | [diff] [blame] | 515 | class SimpleXMLRPCServer(SocketServer.TCPServer, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 516 | SimpleXMLRPCDispatcher): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 517 | """Simple XML-RPC server. |
| 518 | |
| 519 | Simple XML-RPC server that allows functions and a single instance |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 520 | to be installed to handle requests. The default implementation |
| 521 | attempts to dispatch XML-RPC calls to the functions or instance |
| 522 | installed in the server. Override the _dispatch method inhereted |
| 523 | from SimpleXMLRPCDispatcher to change this behavior. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 524 | """ |
| 525 | |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 526 | allow_reuse_address = True |
| 527 | |
Facundo Batista | 7f686fc | 2007-08-17 19:16:44 +0000 | [diff] [blame] | 528 | # Warning: this is for debugging purposes only! Never set this to True in |
| 529 | # production code, as will be sending out sensitive information (exception |
| 530 | # and stack trace details) when exceptions are raised inside |
| 531 | # SimpleXMLRPCRequestHandler.do_POST |
| 532 | _send_traceback_header = False |
| 533 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 534 | def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler, |
Collin Winter | ae04106 | 2007-03-10 14:41:48 +0000 | [diff] [blame] | 535 | logRequests=True, allow_none=False, encoding=None, bind_and_activate=True): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 536 | self.logRequests = logRequests |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 537 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 538 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Georg Brandl | e152a77 | 2008-05-24 18:31:28 +0000 | [diff] [blame] | 539 | SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 540 | |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 541 | # [Bug #1222790] If possible, set close-on-exec flag; if a |
| 542 | # method spawns a subprocess, the subprocess shouldn't have |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 543 | # the listening socket open. |
Anthony Baxter | e29002c | 2006-04-12 12:07:31 +0000 | [diff] [blame] | 544 | if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'): |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 545 | flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD) |
| 546 | flags |= fcntl.FD_CLOEXEC |
| 547 | fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags) |
| 548 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 549 | class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): |
| 550 | """Simple handler for XML-RPC data passed through CGI.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 551 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 552 | def __init__(self, allow_none=False, encoding=None): |
| 553 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 554 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 555 | def handle_xmlrpc(self, request_text): |
| 556 | """Handle a single XML-RPC request""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 557 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 558 | response = self._marshaled_dispatch(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 559 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 560 | print 'Content-Type: text/xml' |
| 561 | print 'Content-Length: %d' % len(response) |
| 562 | print |
Martin v. Löwis | 9c5ea50 | 2003-05-01 05:05:09 +0000 | [diff] [blame] | 563 | sys.stdout.write(response) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 564 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 565 | def handle_get(self): |
| 566 | """Handle a single HTTP GET request. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 567 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 568 | Default implementation indicates an error because |
| 569 | XML-RPC uses the POST method. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 570 | """ |
| 571 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 572 | code = 400 |
| 573 | message, explain = \ |
| 574 | BaseHTTPServer.BaseHTTPRequestHandler.responses[code] |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 575 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 576 | response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \ |
| 577 | { |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 578 | 'code' : code, |
| 579 | 'message' : message, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 580 | 'explain' : explain |
| 581 | } |
| 582 | print 'Status: %d %s' % (code, message) |
| 583 | print 'Content-Type: text/html' |
| 584 | print 'Content-Length: %d' % len(response) |
| 585 | print |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 586 | sys.stdout.write(response) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 587 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 588 | def handle_request(self, request_text = None): |
| 589 | """Handle a single XML-RPC request passed through a CGI post method. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 590 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 591 | If no XML data is given then it is read from stdin. The resulting |
| 592 | XML-RPC response is printed to stdout along with the correct HTTP |
| 593 | headers. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 594 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 595 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 596 | if request_text is None and \ |
| 597 | os.environ.get('REQUEST_METHOD', None) == 'GET': |
| 598 | self.handle_get() |
| 599 | else: |
| 600 | # POST data is normally available through stdin |
| 601 | if request_text is None: |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 602 | request_text = sys.stdin.read() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 603 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 604 | self.handle_xmlrpc(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 605 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 606 | if __name__ == '__main__': |
Andrew M. Kuchling | b0a1e6b | 2006-04-21 12:57:35 +0000 | [diff] [blame] | 607 | print 'Running XML-RPC server on port 8000' |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 608 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 609 | server.register_function(pow) |
| 610 | server.register_function(lambda x,y: x+y, 'add') |
| 611 | server.serve_forever() |