blob: 22889070d931f89a2d12c9dba4b153b19c86a116 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`SimpleXMLRPCServer` --- Basic XML-RPC server
3==================================================
4
5.. module:: SimpleXMLRPCServer
6 :synopsis: Basic XML-RPC server implementation.
7.. moduleauthor:: Brian Quinlan <brianq@activestate.com>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011The :mod:`SimpleXMLRPCServer` module provides a basic server framework for
12XML-RPC servers written in Python. Servers can either be free standing, using
13:class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using
14:class:`CGIXMLRPCRequestHandler`.
15
16
Georg Brandl55ac8f02007-09-01 13:51:09 +000017.. class:: SimpleXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding[, bind_and_activate]]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000018
19 Create a new server instance. This class provides methods for registration of
20 functions that can be called by the XML-RPC protocol. The *requestHandler*
21 parameter should be a factory for request handler instances; it defaults to
22 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
23 are passed to the :class:`SocketServer.TCPServer` constructor. If *logRequests*
24 is true (the default), requests will be logged; setting this parameter to false
25 will turn off logging. The *allow_none* and *encoding* parameters are passed
26 on to :mod:`xmlrpclib` and control the XML-RPC responses that will be returned
27 from the server. The *bind_and_activate* parameter controls whether
28 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
29 constructor; it defaults to true. Setting it to false allows code to manipulate
30 the *allow_reuse_address* class variable before the address is bound.
31
Georg Brandl116aa622007-08-15 14:28:22 +000032
33.. class:: CGIXMLRPCRequestHandler([allow_none[, encoding]])
34
35 Create a new instance to handle XML-RPC requests in a CGI environment. The
36 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpclib` and
37 control the XML-RPC responses that will be returned from the server.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039
40.. class:: SimpleXMLRPCRequestHandler()
41
42 Create a new request handler instance. This request handler supports ``POST``
43 requests and modifies logging so that the *logRequests* parameter to the
44 :class:`SimpleXMLRPCServer` constructor parameter is honored.
45
46
47.. _simple-xmlrpc-servers:
48
49SimpleXMLRPCServer Objects
50--------------------------
51
52The :class:`SimpleXMLRPCServer` class is based on
53:class:`SocketServer.TCPServer` and provides a means of creating simple, stand
54alone XML-RPC servers.
55
56
57.. method:: SimpleXMLRPCServer.register_function(function[, name])
58
59 Register a function that can respond to XML-RPC requests. If *name* is given,
60 it will be the method name associated with *function*, otherwise
61 ``function.__name__`` will be used. *name* can be either a normal or Unicode
62 string, and may contain characters not legal in Python identifiers, including
63 the period character.
64
65
66.. method:: SimpleXMLRPCServer.register_instance(instance[, allow_dotted_names])
67
68 Register an object which is used to expose method names which have not been
69 registered using :meth:`register_function`. If *instance* contains a
70 :meth:`_dispatch` method, it is called with the requested method name and the
71 parameters from the request. Its API is ``def _dispatch(self, method, params)``
72 (note that *params* does not represent a variable argument list). If it calls
73 an underlying function to perform its task, that function is called as
74 ``func(*params)``, expanding the parameter list. The return value from
75 :meth:`_dispatch` is returned to the client as the result. If *instance* does
76 not have a :meth:`_dispatch` method, it is searched for an attribute matching
77 the name of the requested method.
78
79 If the optional *allow_dotted_names* argument is true and the instance does not
80 have a :meth:`_dispatch` method, then if the requested method name contains
81 periods, each component of the method name is searched for individually, with
82 the effect that a simple hierarchical search is performed. The value found from
83 this search is then called with the parameters from the request, and the return
84 value is passed back to the client.
85
86 .. warning::
87
88 Enabling the *allow_dotted_names* option allows intruders to access your
89 module's global variables and may allow intruders to execute arbitrary code on
90 your machine. Only use this option on a secure, closed network.
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. method:: SimpleXMLRPCServer.register_introspection_functions()
94
95 Registers the XML-RPC introspection functions ``system.listMethods``,
96 ``system.methodHelp`` and ``system.methodSignature``.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
99.. method:: SimpleXMLRPCServer.register_multicall_functions()
100
101 Registers the XML-RPC multicall function system.multicall.
102
103
104.. attribute:: SimpleXMLRPCServer.rpc_paths
105
106 An attribute value that must be a tuple listing valid path portions of the URL
107 for receiving XML-RPC requests. Requests posted to other paths will result in a
108 404 "no such page" HTTP error. If this tuple is empty, all paths will be
109 considered valid. The default value is ``('/', '/RPC2')``.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112Example::
113
114 from SimpleXMLRPCServer import SimpleXMLRPCServer
115
116 # Create server
117 server = SimpleXMLRPCServer(("localhost", 8000))
118 server.register_introspection_functions()
119
120 # Register pow() function; this will use the value of
121 # pow.__name__ as the name, which is just 'pow'.
122 server.register_function(pow)
123
124 # Register a function under a different name
125 def adder_function(x,y):
126 return x + y
127 server.register_function(adder_function, 'add')
128
129 # Register an instance; all the methods of the instance are
130 # published as XML-RPC methods (in this case, just 'div').
131 class MyFuncs:
132 def div(self, x, y):
133 return x // y
134
135 server.register_instance(MyFuncs())
136
137 # Run the server's main loop
138 server.serve_forever()
139
140The following client code will call the methods made available by the preceding
141server::
142
143 import xmlrpclib
144
145 s = xmlrpclib.Server('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000146 print(s.pow(2,3)) # Returns 2**3 = 8
147 print(s.add(2,3)) # Returns 5
148 print(s.div(5,2)) # Returns 5//2 = 2
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000151 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154CGIXMLRPCRequestHandler
155-----------------------
156
157The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
158requests sent to Python CGI scripts.
159
160
161.. method:: CGIXMLRPCRequestHandler.register_function(function[, name])
162
163 Register a function that can respond to XML-RPC requests. If *name* is given,
164 it will be the method name associated with function, otherwise
165 *function.__name__* will be used. *name* can be either a normal or Unicode
166 string, and may contain characters not legal in Python identifiers, including
167 the period character.
168
169
170.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
171
172 Register an object which is used to expose method names which have not been
173 registered using :meth:`register_function`. If instance contains a
174 :meth:`_dispatch` method, it is called with the requested method name and the
175 parameters from the request; the return value is returned to the client as the
176 result. If instance does not have a :meth:`_dispatch` method, it is searched
177 for an attribute matching the name of the requested method; if the requested
178 method name contains periods, each component of the method name is searched for
179 individually, with the effect that a simple hierarchical search is performed.
180 The value found from this search is then called with the parameters from the
181 request, and the return value is passed back to the client.
182
183
184.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
185
186 Register the XML-RPC introspection functions ``system.listMethods``,
187 ``system.methodHelp`` and ``system.methodSignature``.
188
189
190.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
191
192 Register the XML-RPC multicall function ``system.multicall``.
193
194
195.. method:: CGIXMLRPCRequestHandler.handle_request([request_text = None])
196
197 Handle a XML-RPC request. If *request_text* is given, it should be the POST
198 data provided by the HTTP server, otherwise the contents of stdin will be used.
199
200Example::
201
202 class MyFuncs:
203 def div(self, x, y) : return x // y
204
205
206 handler = CGIXMLRPCRequestHandler()
207 handler.register_function(pow)
208 handler.register_function(lambda x,y: x+y, 'add')
209 handler.register_introspection_functions()
210 handler.register_instance(MyFuncs())
211 handler.handle_request()
212