/Doc/library/simplexmlrpcserver.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 246 lines · 166 code · 80 blank · 0 comment · 0 complexity · c0494551dd2b02c0ba87facc2e391cc6 MD5 · raw file

  1. :mod:`SimpleXMLRPCServer` --- Basic XML-RPC server
  2. ==================================================
  3. .. module:: SimpleXMLRPCServer
  4. :synopsis: Basic XML-RPC server implementation.
  5. .. moduleauthor:: Brian Quinlan <brianq@activestate.com>
  6. .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
  7. .. note::
  8. The :mod:`SimpleXMLRPCServer` module has been merged into
  9. :mod:`xmlrpc.server` in Python 3.0. The :term:`2to3` tool will automatically
  10. adapt imports when converting your sources to 3.0.
  11. .. versionadded:: 2.2
  12. The :mod:`SimpleXMLRPCServer` module provides a basic server framework for
  13. XML-RPC servers written in Python. Servers can either be free standing, using
  14. :class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using
  15. :class:`CGIXMLRPCRequestHandler`.
  16. .. class:: SimpleXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding]]]])
  17. Create a new server instance. This class provides methods for registration of
  18. functions that can be called by the XML-RPC protocol. The *requestHandler*
  19. parameter should be a factory for request handler instances; it defaults to
  20. :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
  21. are passed to the :class:`SocketServer.TCPServer` constructor. If *logRequests*
  22. is true (the default), requests will be logged; setting this parameter to false
  23. will turn off logging. The *allow_none* and *encoding* parameters are passed
  24. on to :mod:`xmlrpclib` and control the XML-RPC responses that will be returned
  25. from the server. The *bind_and_activate* parameter controls whether
  26. :meth:`server_bind` and :meth:`server_activate` are called immediately by the
  27. constructor; it defaults to true. Setting it to false allows code to manipulate
  28. the *allow_reuse_address* class variable before the address is bound.
  29. .. versionchanged:: 2.5
  30. The *allow_none* and *encoding* parameters were added.
  31. .. versionchanged:: 2.6
  32. The *bind_and_activate* parameter was added.
  33. .. class:: CGIXMLRPCRequestHandler([allow_none[, encoding]])
  34. Create a new instance to handle XML-RPC requests in a CGI environment. The
  35. *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpclib` and
  36. control the XML-RPC responses that will be returned from the server.
  37. .. versionadded:: 2.3
  38. .. versionchanged:: 2.5
  39. The *allow_none* and *encoding* parameters were added.
  40. .. class:: SimpleXMLRPCRequestHandler()
  41. Create a new request handler instance. This request handler supports ``POST``
  42. requests and modifies logging so that the *logRequests* parameter to the
  43. :class:`SimpleXMLRPCServer` constructor parameter is honored.
  44. .. _simple-xmlrpc-servers:
  45. SimpleXMLRPCServer Objects
  46. --------------------------
  47. The :class:`SimpleXMLRPCServer` class is based on
  48. :class:`SocketServer.TCPServer` and provides a means of creating simple, stand
  49. alone XML-RPC servers.
  50. .. method:: SimpleXMLRPCServer.register_function(function[, name])
  51. Register a function that can respond to XML-RPC requests. If *name* is given,
  52. it will be the method name associated with *function*, otherwise
  53. ``function.__name__`` will be used. *name* can be either a normal or Unicode
  54. string, and may contain characters not legal in Python identifiers, including
  55. the period character.
  56. .. method:: SimpleXMLRPCServer.register_instance(instance[, allow_dotted_names])
  57. Register an object which is used to expose method names which have not been
  58. registered using :meth:`register_function`. If *instance* contains a
  59. :meth:`_dispatch` method, it is called with the requested method name and the
  60. parameters from the request. Its API is ``def _dispatch(self, method, params)``
  61. (note that *params* does not represent a variable argument list). If it calls
  62. an underlying function to perform its task, that function is called as
  63. ``func(*params)``, expanding the parameter list. The return value from
  64. :meth:`_dispatch` is returned to the client as the result. If *instance* does
  65. not have a :meth:`_dispatch` method, it is searched for an attribute matching
  66. the name of the requested method.
  67. If the optional *allow_dotted_names* argument is true and the instance does not
  68. have a :meth:`_dispatch` method, then if the requested method name contains
  69. periods, each component of the method name is searched for individually, with
  70. the effect that a simple hierarchical search is performed. The value found from
  71. this search is then called with the parameters from the request, and the return
  72. value is passed back to the client.
  73. .. warning::
  74. Enabling the *allow_dotted_names* option allows intruders to access your
  75. module's global variables and may allow intruders to execute arbitrary code on
  76. your machine. Only use this option on a secure, closed network.
  77. .. versionchanged:: 2.3.5, 2.4.1
  78. *allow_dotted_names* was added to plug a security hole; prior versions are
  79. insecure.
  80. .. method:: SimpleXMLRPCServer.register_introspection_functions()
  81. Registers the XML-RPC introspection functions ``system.listMethods``,
  82. ``system.methodHelp`` and ``system.methodSignature``.
  83. .. versionadded:: 2.3
  84. .. method:: SimpleXMLRPCServer.register_multicall_functions()
  85. Registers the XML-RPC multicall function system.multicall.
  86. .. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
  87. An attribute value that must be a tuple listing valid path portions of the URL
  88. for receiving XML-RPC requests. Requests posted to other paths will result in a
  89. 404 "no such page" HTTP error. If this tuple is empty, all paths will be
  90. considered valid. The default value is ``('/', '/RPC2')``.
  91. .. versionadded:: 2.5
  92. .. _simplexmlrpcserver-example:
  93. SimpleXMLRPCServer Example
  94. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  95. Server code::
  96. from SimpleXMLRPCServer import SimpleXMLRPCServer
  97. from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
  98. # Restrict to a particular path.
  99. class RequestHandler(SimpleXMLRPCRequestHandler):
  100. rpc_paths = ('/RPC2',)
  101. # Create server
  102. server = SimpleXMLRPCServer(("localhost", 8000),
  103. requestHandler=RequestHandler)
  104. server.register_introspection_functions()
  105. # Register pow() function; this will use the value of
  106. # pow.__name__ as the name, which is just 'pow'.
  107. server.register_function(pow)
  108. # Register a function under a different name
  109. def adder_function(x,y):
  110. return x + y
  111. server.register_function(adder_function, 'add')
  112. # Register an instance; all the methods of the instance are
  113. # published as XML-RPC methods (in this case, just 'div').
  114. class MyFuncs:
  115. def div(self, x, y):
  116. return x // y
  117. server.register_instance(MyFuncs())
  118. # Run the server's main loop
  119. server.serve_forever()
  120. The following client code will call the methods made available by the preceding
  121. server::
  122. import xmlrpclib
  123. s = xmlrpclib.ServerProxy('http://localhost:8000')
  124. print s.pow(2,3) # Returns 2**3 = 8
  125. print s.add(2,3) # Returns 5
  126. print s.div(5,2) # Returns 5//2 = 2
  127. # Print list of available methods
  128. print s.system.listMethods()
  129. CGIXMLRPCRequestHandler
  130. -----------------------
  131. The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
  132. requests sent to Python CGI scripts.
  133. .. method:: CGIXMLRPCRequestHandler.register_function(function[, name])
  134. Register a function that can respond to XML-RPC requests. If *name* is given,
  135. it will be the method name associated with function, otherwise
  136. *function.__name__* will be used. *name* can be either a normal or Unicode
  137. string, and may contain characters not legal in Python identifiers, including
  138. the period character.
  139. .. method:: CGIXMLRPCRequestHandler.register_instance(instance)
  140. Register an object which is used to expose method names which have not been
  141. registered using :meth:`register_function`. If instance contains a
  142. :meth:`_dispatch` method, it is called with the requested method name and the
  143. parameters from the request; the return value is returned to the client as the
  144. result. If instance does not have a :meth:`_dispatch` method, it is searched
  145. for an attribute matching the name of the requested method; if the requested
  146. method name contains periods, each component of the method name is searched for
  147. individually, with the effect that a simple hierarchical search is performed.
  148. The value found from this search is then called with the parameters from the
  149. request, and the return value is passed back to the client.
  150. .. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
  151. Register the XML-RPC introspection functions ``system.listMethods``,
  152. ``system.methodHelp`` and ``system.methodSignature``.
  153. .. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
  154. Register the XML-RPC multicall function ``system.multicall``.
  155. .. method:: CGIXMLRPCRequestHandler.handle_request([request_text = None])
  156. Handle a XML-RPC request. If *request_text* is given, it should be the POST
  157. data provided by the HTTP server, otherwise the contents of stdin will be used.
  158. Example::
  159. class MyFuncs:
  160. def div(self, x, y) : return x // y
  161. handler = CGIXMLRPCRequestHandler()
  162. handler.register_function(pow)
  163. handler.register_function(lambda x,y: x+y, 'add')
  164. handler.register_introspection_functions()
  165. handler.register_instance(MyFuncs())
  166. handler.handle_request()