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