blob: fdde60c7ce46cc3408f32dc3040ba09021ddc1fa [file] [log] [blame]
Fredrik Lundhb329b712001-09-17 17:35:21 +00001"""Simple XML-RPC Server.
2
3This module can be used to create simple XML-RPC servers
4by creating a server and either installing functions, a
Martin v. Löwisd69663d2003-01-15 11:37:23 +00005class instance, or by extending the SimpleXMLRPCServer
Fredrik Lundhb329b712001-09-17 17:35:21 +00006class.
7
Martin v. Löwisd69663d2003-01-15 11:37:23 +00008It can also be used to handle XML-RPC requests in a CGI
9environment using CGIXMLRPCRequestHandler.
10
Fredrik Lundhb329b712001-09-17 17:35:21 +000011A list of possible usage patterns follows:
12
131. Install functions:
14
15server = SimpleXMLRPCServer(("localhost", 8000))
16server.register_function(pow)
17server.register_function(lambda x,y: x+y, 'add')
18server.serve_forever()
19
202. Install an instance:
21
22class 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öwisd69663d2003-01-15 11:37:23 +000028 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 Lundhb329b712001-09-17 17:35:21 +000033 def pow(self, x, y): return pow(x, y)
34 def add(self, x, y) : return x + y
Tim Peters2c60f7a2003-01-29 03:49:43 +000035
Fredrik Lundhb329b712001-09-17 17:35:21 +000036server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +000037server.register_introspection_functions()
Fredrik Lundhb329b712001-09-17 17:35:21 +000038server.register_instance(MyFuncs())
39server.serve_forever()
40
413. Install an instance with custom dispatch method:
42
43class Math:
Martin v. Löwisd69663d2003-01-15 11:37:23 +000044 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 Lundhb329b712001-09-17 17:35:21 +000059 def _dispatch(self, method, params):
60 if method == 'pow':
Martin v. Löwisd69663d2003-01-15 11:37:23 +000061 return pow(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +000062 elif method == 'add':
63 return params[0] + params[1]
64 else:
65 raise 'bad method'
Martin v. Löwisd69663d2003-01-15 11:37:23 +000066
Fredrik Lundhb329b712001-09-17 17:35:21 +000067server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +000068server.register_introspection_functions()
Fredrik Lundhb329b712001-09-17 17:35:21 +000069server.register_instance(Math())
70server.serve_forever()
71
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000724. Subclass SimpleXMLRPCServer:
Fredrik Lundhb329b712001-09-17 17:35:21 +000073
Martin v. Löwisd69663d2003-01-15 11:37:23 +000074class MathServer(SimpleXMLRPCServer):
Fredrik Lundhb329b712001-09-17 17:35:21 +000075 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öwisd69663d2003-01-15 11:37:23 +000084 return func(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +000085
86 def export_add(self, x, y):
87 return x + y
88
Martin v. Löwisd69663d2003-01-15 11:37:23 +000089server = MathServer(("localhost", 8000))
Fredrik Lundhb329b712001-09-17 17:35:21 +000090server.serve_forever()
Martin v. Löwisd69663d2003-01-15 11:37:23 +000091
925. CGI script:
93
94server = CGIXMLRPCRequestHandler()
95server.register_function(pow)
96server.handle_request()
Fredrik Lundhb329b712001-09-17 17:35:21 +000097"""
98
99# Written by Brian Quinlan (brian@sweetapp.com).
100# Based on code written by Fredrik Lundh.
101
102import xmlrpclib
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000103from xmlrpclib import Fault
Fredrik Lundhb329b712001-09-17 17:35:21 +0000104import SocketServer
105import BaseHTTPServer
106import sys
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000107import os
Fredrik Lundhb329b712001-09-17 17:35:21 +0000108
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000109def resolve_dotted_attribute(obj, attr):
110 """resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d
Fredrik Lundhb329b712001-09-17 17:35:21 +0000111
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000112 Resolves a dotted attribute name to an object. Raises
113 an AttributeError if any attribute in the chain starts with a '_'.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000114 """
115
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000116 for i in attr.split('.'):
117 if i.startswith('_'):
118 raise AttributeError(
119 'attempt to access private attribute "%s"' % i
120 )
121 else:
122 obj = getattr(obj,i)
123 return obj
Fredrik Lundhb329b712001-09-17 17:35:21 +0000124
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000125def list_public_methods(obj):
126 """Returns a list of attribute strings, found in the specified
127 object, which represent callable attributes"""
128
129 return [member for member in dir(obj)
130 if not member.startswith('_') and
131 callable(getattr(obj, member))]
132
133def remove_duplicates(lst):
134 """remove_duplicates([2,2,2,1,3,3]) => [3,1,2]
135
136 Returns a copy of a list without duplicates. Every list
137 item must be hashable and the order of the items in the
138 resulting list is not defined.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000139 """
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000140 u = {}
141 for x in lst:
142 u[x] = 1
143
144 return u.keys()
145
146class SimpleXMLRPCDispatcher:
147 """Mix-in class that dispatches XML-RPC requests.
148
149 This class is used to register XML-RPC method handlers
150 and then to dispatch them. There should never be any
151 reason to instantiate this class directly.
152 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000153
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000154 def __init__(self):
155 self.funcs = {}
156 self.instance = None
157
158 def register_instance(self, instance):
159 """Registers an instance to respond to XML-RPC requests.
160
161 Only one instance can be installed at a time.
162
163 If the registered instance has a _dispatch method then that
164 method will be called with the name of the XML-RPC method and
165 it's parameters as a tuple
166 e.g. instance._dispatch('add',(2,3))
167
168 If the registered instance does not have a _dispatch method
169 then the instance will be searched to find a matching method
170 and, if found, will be called. Methods beginning with an '_'
171 are considered private and will not be called by
172 SimpleXMLRPCServer.
173
174 If a registered function matches a XML-RPC request, then it
175 will be called instead of the registered instance.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000176 """
177
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000178 self.instance = instance
179
180 def register_function(self, function, name = None):
181 """Registers a function to respond to XML-RPC requests.
182
183 The optional name argument can be used to set a Unicode name
184 for the function.
185 """
186
187 if name is None:
188 name = function.__name__
189 self.funcs[name] = function
190
191 def register_introspection_functions(self):
192 """Registers the XML-RPC introspection methods in the system
193 namespace.
194
195 see http://xmlrpc.usefulinc.com/doc/reserved.html
196 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000197
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000198 self.funcs.update({'system.listMethods' : self.system_listMethods,
199 'system.methodSignature' : self.system_methodSignature,
200 'system.methodHelp' : self.system_methodHelp})
201
202 def register_multicall_functions(self):
203 """Registers the XML-RPC multicall method in the system
204 namespace.
205
206 see http://www.xmlrpc.com/discuss/msgReader$1208"""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000207
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000208 self.funcs.update({'system.multicall' : self.system_multicall})
Tim Peters2c60f7a2003-01-29 03:49:43 +0000209
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000210 def _marshaled_dispatch(self, data, dispatch_method = None):
211 """Dispatches an XML-RPC method from marshalled (XML) data.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000212
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000213 XML-RPC methods are dispatched from the marshalled (XML) data
214 using the _dispatch method and the result is returned as
215 marshalled data. For backwards compatibility, a dispatch
Tim Peters2c60f7a2003-01-29 03:49:43 +0000216 function can be provided as an argument (see comment in
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000217 SimpleXMLRPCRequestHandler.do_POST) but overriding the
218 existing method through subclassing is the prefered means
219 of changing method dispatch behavior.
220 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000221
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000222 params, method = xmlrpclib.loads(data)
223
224 # generate response
Fredrik Lundhb329b712001-09-17 17:35:21 +0000225 try:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000226 if dispatch_method is not None:
227 response = dispatch_method(method, params)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000228 else:
Fredrik Lundhb329b712001-09-17 17:35:21 +0000229 response = self._dispatch(method, params)
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000230 # wrap response in a singleton tuple
231 response = (response,)
232 response = xmlrpclib.dumps(response, methodresponse=1)
233 except Fault, fault:
234 response = xmlrpclib.dumps(fault)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000235 except:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000236 # report exception back to server
237 response = xmlrpclib.dumps(
238 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
239 )
240
241 return response
242
243 def system_listMethods(self):
244 """system.listMethods() => ['add', 'subtract', 'multiple']
245
246 Returns a list of the methods supported by the server."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000247
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000248 methods = self.funcs.keys()
249 if self.instance is not None:
250 # Instance can implement _listMethod to return a list of
251 # methods
252 if hasattr(self.instance, '_listMethods'):
253 methods = remove_duplicates(
254 methods + self.instance._listMethods()
255 )
256 # if the instance has a _dispatch method then we
257 # don't have enough information to provide a list
258 # of methods
259 elif not hasattr(self.instance, '_dispatch'):
260 methods = remove_duplicates(
261 methods + list_public_methods(self.instance)
262 )
263 methods.sort()
264 return methods
Tim Peters2c60f7a2003-01-29 03:49:43 +0000265
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000266 def system_methodSignature(self, method_name):
267 """system.methodSignature('add') => [double, int, int]
268
269 Returns a list describing the signiture of the method. In the
270 above example, the add method takes two integers as arguments
271 and returns a double result.
272
273 This server does NOT support system.methodSignature."""
274
275 # See http://xmlrpc.usefulinc.com/doc/sysmethodsig.html
Tim Peters2c60f7a2003-01-29 03:49:43 +0000276
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000277 return 'signatures not supported'
278
279 def system_methodHelp(self, method_name):
280 """system.methodHelp('add') => "Adds two integers together"
281
282 Returns a string containing documentation for the specified method."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000283
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000284 method = None
285 if self.funcs.has_key(method_name):
286 method = self.funcs[method_name]
287 elif self.instance is not None:
288 # Instance can implement _methodHelp to return help for a method
289 if hasattr(self.instance, '_methodHelp'):
290 return self.instance._methodHelp(method_name)
291 # if the instance has a _dispatch method then we
292 # don't have enough information to provide help
293 elif not hasattr(self.instance, '_dispatch'):
294 try:
295 method = resolve_dotted_attribute(
296 self.instance,
297 method_name
298 )
299 except AttributeError:
300 pass
301
302 # Note that we aren't checking that the method actually
303 # be a callable object of some kind
304 if method is None:
305 return ""
Fredrik Lundhb329b712001-09-17 17:35:21 +0000306 else:
Neal Norwitz732911f2003-06-29 04:16:28 +0000307 import pydoc
Neal Norwitz3f401f02003-06-29 04:19:37 +0000308 return pydoc.getdoc(method)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000309
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000310 def system_multicall(self, call_list):
311 """system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => \
312[[4], ...]
Fredrik Lundhb329b712001-09-17 17:35:21 +0000313
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000314 Allows the caller to package multiple XML-RPC calls into a single
315 request.
316
Tim Peters2c60f7a2003-01-29 03:49:43 +0000317 See http://www.xmlrpc.com/discuss/msgReader$1208
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000318 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000319
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000320 results = []
321 for call in call_list:
322 method_name = call['methodName']
323 params = call['params']
324
325 try:
326 # XXX A marshalling error in any response will fail the entire
327 # multicall. If someone cares they should fix this.
328 results.append([self._dispatch(method_name, params)])
329 except Fault, fault:
330 results.append(
331 {'faultCode' : fault.faultCode,
332 'faultString' : fault.faultString}
333 )
334 except:
335 results.append(
336 {'faultCode' : 1,
337 'faultString' : "%s:%s" % (sys.exc_type, sys.exc_value)}
338 )
339 return results
Tim Peters2c60f7a2003-01-29 03:49:43 +0000340
Fredrik Lundhb329b712001-09-17 17:35:21 +0000341 def _dispatch(self, method, params):
342 """Dispatches the XML-RPC method.
343
344 XML-RPC calls are forwarded to a registered function that
345 matches the called XML-RPC method name. If no such function
346 exists then the call is forwarded to the registered instance,
347 if available.
348
349 If the registered instance has a _dispatch method then that
350 method will be called with the name of the XML-RPC method and
351 it's parameters as a tuple
352 e.g. instance._dispatch('add',(2,3))
353
354 If the registered instance does not have a _dispatch method
355 then the instance will be searched to find a matching method
356 and, if found, will be called.
357
358 Methods beginning with an '_' are considered private and will
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000359 not be called.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000360 """
361
Fredrik Lundhb329b712001-09-17 17:35:21 +0000362 func = None
363 try:
364 # check to see if a matching function has been registered
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000365 func = self.funcs[method]
Fredrik Lundhb329b712001-09-17 17:35:21 +0000366 except KeyError:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000367 if self.instance is not None:
Fredrik Lundhb329b712001-09-17 17:35:21 +0000368 # check for a _dispatch method
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000369 if hasattr(self.instance, '_dispatch'):
370 return self.instance._dispatch(method, params)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000371 else:
372 # call instance method directly
373 try:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000374 func = resolve_dotted_attribute(
375 self.instance,
Fredrik Lundhb329b712001-09-17 17:35:21 +0000376 method
377 )
378 except AttributeError:
379 pass
380
381 if func is not None:
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000382 return func(*params)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000383 else:
384 raise Exception('method "%s" is not supported' % method)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000385
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000386class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
387 """Simple XML-RPC request handler class.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000388
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000389 Handles all HTTP POST requests and attempts to decode them as
390 XML-RPC requests.
391 """
392
393 def do_POST(self):
394 """Handles the HTTP POST request.
395
396 Attempts to interpret all HTTP POST requests as XML-RPC calls,
397 which are forwarded to the server's _dispatch method for handling.
398 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000399
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000400 try:
401 # get arguments
402 data = self.rfile.read(int(self.headers["content-length"]))
403 # In previous versions of SimpleXMLRPCServer, _dispatch
404 # could be overridden in this class, instead of in
405 # SimpleXMLRPCDispatcher. To maintain backwards compatibility,
406 # check to see if a subclass implements _dispatch and dispatch
407 # using that method if present.
408 response = self.server._marshaled_dispatch(
409 data, getattr(self, '_dispatch', None)
410 )
411 except: # This should only happen if the module is buggy
412 # internal error, report as HTTP server error
413 self.send_response(500)
414 self.end_headers()
415 else:
416 # got a valid XML RPC response
417 self.send_response(200)
418 self.send_header("Content-type", "text/xml")
419 self.send_header("Content-length", str(len(response)))
420 self.end_headers()
421 self.wfile.write(response)
422
423 # shut down the connection
424 self.wfile.flush()
425 self.connection.shutdown(1)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000426
Fredrik Lundhb329b712001-09-17 17:35:21 +0000427 def log_request(self, code='-', size='-'):
428 """Selectively log an accepted request."""
429
430 if self.server.logRequests:
431 BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
432
Tim Peters2c60f7a2003-01-29 03:49:43 +0000433class SimpleXMLRPCServer(SocketServer.TCPServer,
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000434 SimpleXMLRPCDispatcher):
Fredrik Lundhb329b712001-09-17 17:35:21 +0000435 """Simple XML-RPC server.
436
437 Simple XML-RPC server that allows functions and a single instance
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000438 to be installed to handle requests. The default implementation
439 attempts to dispatch XML-RPC calls to the functions or instance
440 installed in the server. Override the _dispatch method inhereted
441 from SimpleXMLRPCDispatcher to change this behavior.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000442 """
443
444 def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
445 logRequests=1):
Fredrik Lundhb329b712001-09-17 17:35:21 +0000446 self.logRequests = logRequests
Tim Peters2c60f7a2003-01-29 03:49:43 +0000447
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000448 SimpleXMLRPCDispatcher.__init__(self)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000449 SocketServer.TCPServer.__init__(self, addr, requestHandler)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000450
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000451class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher):
452 """Simple handler for XML-RPC data passed through CGI."""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000453
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000454 def __init__(self):
455 SimpleXMLRPCDispatcher.__init__(self)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000456
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000457 def handle_xmlrpc(self, request_text):
458 """Handle a single XML-RPC request"""
Tim Peters2c60f7a2003-01-29 03:49:43 +0000459
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000460 response = self._marshaled_dispatch(request_text)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000461
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000462 print 'Content-Type: text/xml'
463 print 'Content-Length: %d' % len(response)
464 print
Martin v. Löwis9c5ea502003-05-01 05:05:09 +0000465 sys.stdout.write(response)
Fredrik Lundhb329b712001-09-17 17:35:21 +0000466
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000467 def handle_get(self):
468 """Handle a single HTTP GET request.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000469
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000470 Default implementation indicates an error because
471 XML-RPC uses the POST method.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000472 """
473
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000474 code = 400
475 message, explain = \
476 BaseHTTPServer.BaseHTTPRequestHandler.responses[code]
Tim Peters2c60f7a2003-01-29 03:49:43 +0000477
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000478 response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \
479 {
Tim Peters2c60f7a2003-01-29 03:49:43 +0000480 'code' : code,
481 'message' : message,
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000482 'explain' : explain
483 }
484 print 'Status: %d %s' % (code, message)
485 print 'Content-Type: text/html'
486 print 'Content-Length: %d' % len(response)
487 print
Neal Norwitz732911f2003-06-29 04:16:28 +0000488 sys.stdout.write(response)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000489
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000490 def handle_request(self, request_text = None):
491 """Handle a single XML-RPC request passed through a CGI post method.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000492
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000493 If no XML data is given then it is read from stdin. The resulting
494 XML-RPC response is printed to stdout along with the correct HTTP
495 headers.
Fredrik Lundhb329b712001-09-17 17:35:21 +0000496 """
Tim Peters2c60f7a2003-01-29 03:49:43 +0000497
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000498 if request_text is None and \
499 os.environ.get('REQUEST_METHOD', None) == 'GET':
500 self.handle_get()
501 else:
502 # POST data is normally available through stdin
503 if request_text is None:
Tim Peters2c60f7a2003-01-29 03:49:43 +0000504 request_text = sys.stdin.read()
Fredrik Lundhb329b712001-09-17 17:35:21 +0000505
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000506 self.handle_xmlrpc(request_text)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000507
Fredrik Lundhb329b712001-09-17 17:35:21 +0000508if __name__ == '__main__':
509 server = SimpleXMLRPCServer(("localhost", 8000))
510 server.register_function(pow)
511 server.register_function(lambda x,y: x+y, 'add')
512 server.serve_forever()