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