blob: 235905e71080d0bb166feb20b93e8afef83cd1d4 [file] [log] [blame]
Fred Drakee486e0d2001-09-28 22:02:21 +00001\section{\module{SimpleXMLRPCServer} ---
2 Basic XML-RPC server}
3
4\declaremodule{standard}{SimpleXMLRPCServer}
Fred Drake48704ee2001-11-28 07:32:53 +00005\modulesynopsis{Basic XML-RPC server implementation.}
Fred Drakee486e0d2001-09-28 22:02:21 +00006\moduleauthor{Brian Quinlan}{brianq@activestate.com}
7\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
8
Fredrik Lundh5b093a02006-01-11 00:18:43 +00009\versionadded{2.2}
Fred Drakee486e0d2001-09-28 22:02:21 +000010
11The \module{SimpleXMLRPCServer} module provides a basic server
Martin v. Löwisd69663d2003-01-15 11:37:23 +000012framework for XML-RPC servers written in Python. Servers can either
13be free standing, using \class{SimpleXMLRPCServer}, or embedded in a
14CGI environment, using \class{CGIXMLRPCRequestHandler}.
Fred Drakee486e0d2001-09-28 22:02:21 +000015
16\begin{classdesc}{SimpleXMLRPCServer}{addr\optional{,
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +000017 requestHandler\optional{,
Collin Winterae041062007-03-10 14:41:48 +000018 logRequests\optional{,
19 allow_none\optional{,
20 encoding}}}}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000021
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000022 Create a new server instance. This class
23 provides methods for registration of functions that can be called by
24 the XML-RPC protocol. The \var{requestHandler} parameter
Fred Drakee486e0d2001-09-28 22:02:21 +000025 should be a factory for request handler instances; it defaults to
26 \class{SimpleXMLRPCRequestHandler}. The \var{addr} and
27 \var{requestHandler} parameters are passed to the
28 \class{\refmodule{SocketServer}.TCPServer} constructor. If
29 \var{logRequests} is true (the default), requests will be logged;
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000030 setting this parameter to false will turn off logging.
31 The \var{allow_none} and \var{encoding} parameters are passed on to
32 \module{xmlrpclib} and control the XML-RPC responses that will be returned
Collin Winterae041062007-03-10 14:41:48 +000033 from the server. The \var{bind_and_activate} parameter controls whether
34 \method{server_bind()} and \method{server_activate()} are called immediately
35 by the constructor; it defaults to true. Setting it to false allows code to
36 manipulate the \var{allow_reuse_address} class variable before the address
37 is bound.
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000038 \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5}
Collin Winterae041062007-03-10 14:41:48 +000039 \versionchanged[The \var{bind_and_activate} parameter was added]{2.6}
Fred Drakee486e0d2001-09-28 22:02:21 +000040\end{classdesc}
41
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000042\begin{classdesc}{CGIXMLRPCRequestHandler}{\optional{allow_none\optional{, encoding}}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000043 Create a new instance to handle XML-RPC requests in a CGI
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000044 environment.
45 The \var{allow_none} and \var{encoding} parameters are passed on to
46 \module{xmlrpclib} and control the XML-RPC responses that will be returned
47 from the server.
48 \versionadded{2.3}
49 \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000050\end{classdesc}
Fred Drakee486e0d2001-09-28 22:02:21 +000051
52\begin{classdesc}{SimpleXMLRPCRequestHandler}{}
53 Create a new request handler instance. This request handler
54 supports \code{POST} requests and modifies logging so that the
55 \var{logRequests} parameter to the \class{SimpleXMLRPCServer}
56 constructor parameter is honored.
57\end{classdesc}
58
59
60\subsection{SimpleXMLRPCServer Objects \label{simple-xmlrpc-servers}}
61
Martin v. Löwisd69663d2003-01-15 11:37:23 +000062The \class{SimpleXMLRPCServer} class is based on
63\class{SocketServer.TCPServer} and provides a means of creating
64simple, stand alone XML-RPC servers.
Fred Drakee486e0d2001-09-28 22:02:21 +000065
66\begin{methoddesc}[SimpleXMLRPCServer]{register_function}{function\optional{,
67 name}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000068 Register a function that can respond to XML-RPC requests. If
69 \var{name} is given, it will be the method name associated with
70 \var{function}, otherwise \code{\var{function}.__name__} will be
71 used. \var{name} can be either a normal or Unicode string, and may
72 contain characters not legal in Python identifiers, including the
73 period character.
Fred Drakee486e0d2001-09-28 22:02:21 +000074\end{methoddesc}
75
Guido van Rossumd0641422005-02-03 15:01:24 +000076\begin{methoddesc}[SimpleXMLRPCServer]{register_instance}{instance\optional{,
77 allow_dotted_names}}
Fred Drakee486e0d2001-09-28 22:02:21 +000078 Register an object which is used to expose method names which have
79 not been registered using \method{register_function()}. If
80 \var{instance} contains a \method{_dispatch()} method, it is called
Skip Montanaro0bbf1372004-09-03 00:04:05 +000081 with the requested method name and the parameters from the request. Its
Walter Dörwaldcca3af32005-08-18 19:40:39 +000082 API is \code{def \method{_dispatch}(self, method, params)} (note that
Skip Montanaro0bbf1372004-09-03 00:04:05 +000083 \var{params} does not represent a variable argument list). If it calls an
84 underlying function to perform its task, that function is called as
85 \code{func(*params)}, expanding the parameter list.
86 The return value from \method{_dispatch()} is returned to the client as
87 the result. If
Fred Drakee486e0d2001-09-28 22:02:21 +000088 \var{instance} does not have a \method{_dispatch()} method, it is
Guido van Rossumd0641422005-02-03 15:01:24 +000089 searched for an attribute matching the name of the requested method.
90
91 If the optional \var{allow_dotted_names} argument is true and the
92 instance does not have a \method{_dispatch()} method, then
Fred Drakee486e0d2001-09-28 22:02:21 +000093 if the requested method name contains periods, each component of the
94 method name is searched for individually, with the effect that a
95 simple hierarchical search is performed. The value found from this
96 search is then called with the parameters from the request, and the
97 return value is passed back to the client.
Guido van Rossumd0641422005-02-03 15:01:24 +000098
99 \begin{notice}[warning]
100 Enabling the \var{allow_dotted_names} option allows intruders to access
101 your module's global variables and may allow intruders to execute
102 arbitrary code on your machine. Only use this option on a secure,
103 closed network.
104 \end{notice}
105
106 \versionchanged[\var{allow_dotted_names} was added to plug a security hole;
107 prior versions are insecure]{2.3.5, 2.4.1}
108
Fred Drakee486e0d2001-09-28 22:02:21 +0000109\end{methoddesc}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000110
Georg Brandlae91afd2007-04-01 22:39:10 +0000111\begin{methoddesc}[SimpleXMLRPCServer]{register_introspection_functions}{}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000112 Registers the XML-RPC introspection functions \code{system.listMethods},
113 \code{system.methodHelp} and \code{system.methodSignature}.
114 \versionadded{2.3}
115\end{methoddesc}
116
Georg Brandlae91afd2007-04-01 22:39:10 +0000117\begin{methoddesc}[SimpleXMLRPCServer]{register_multicall_functions}{}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000118 Registers the XML-RPC multicall function system.multicall.
119\end{methoddesc}
120
Andrew M. Kuchling622f1442006-05-31 14:08:48 +0000121\begin{memberdesc}[SimpleXMLRPCServer]{rpc_paths}
122An attribute value that must be a tuple listing valid path portions of
123the URL for receiving XML-RPC requests. Requests posted to other
124paths will result in a 404 ``no such page'' HTTP error. If this
125tuple is empty, all paths will be considered valid.
126The default value is \code{('/', '/RPC2')}.
127 \versionadded{2.5}
128\end{memberdesc}
129
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000130Example:
131
132\begin{verbatim}
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000133from SimpleXMLRPCServer import SimpleXMLRPCServer
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000134
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000135# Create server
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000136server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000137server.register_introspection_functions()
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000138
139# Register pow() function; this will use the value of
140# pow.__name__ as the name, which is just 'pow'.
141server.register_function(pow)
142
143# Register a function under a different name
144def adder_function(x,y):
145 return x + y
146server.register_function(adder_function, 'add')
147
148# Register an instance; all the methods of the instance are
149# published as XML-RPC methods (in this case, just 'div').
150class MyFuncs:
151 def div(self, x, y):
152 return x // y
153
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000154server.register_instance(MyFuncs())
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000155
156# Run the server's main loop
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000157server.serve_forever()
158\end{verbatim}
159
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000160The following client code will call the methods made available by
161the preceding server:
162
163\begin{verbatim}
164import xmlrpclib
165
166s = xmlrpclib.Server('http://localhost:8000')
167print s.pow(2,3) # Returns 2**3 = 8
168print s.add(2,3) # Returns 5
169print s.div(5,2) # Returns 5//2 = 2
170
171# Print list of available methods
172print s.system.listMethods()
173\end{verbatim}
174
175
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000176\subsection{CGIXMLRPCRequestHandler}
177
178The \class{CGIXMLRPCRequestHandler} class can be used to
179handle XML-RPC requests sent to Python CGI scripts.
180
Georg Brandlae91afd2007-04-01 22:39:10 +0000181\begin{methoddesc}[CGIXMLRPCRequestHandler]{register_function}{function\optional{, name}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000182Register a function that can respond to XML-RPC requests. If
Fred Drake42b567f2003-01-17 22:47:33 +0000183\var{name} is given, it will be the method name associated with
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000184function, otherwise \var{function.__name__} will be used. \var{name}
185can be either a normal or Unicode string, and may contain
186characters not legal in Python identifiers, including the period
187character.
188\end{methoddesc}
189
Georg Brandlae91afd2007-04-01 22:39:10 +0000190\begin{methoddesc}[CGIXMLRPCRequestHandler]{register_instance}{instance}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000191Register an object which is used to expose method names
192which have not been registered using \method{register_function()}. If
193instance contains a \method{_dispatch()} method, it is called with the
194requested method name and the parameters from the
195request; the return value is returned to the client as the result.
Fred Drake42b567f2003-01-17 22:47:33 +0000196If instance does not have a \method{_dispatch()} method, it is searched
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000197for an attribute matching the name of the requested method; if
198the requested method name contains periods, each
199component of the method name is searched for individually,
200with the effect that a simple hierarchical search is performed.
201The value found from this search is then called with the
202parameters from the request, and the return value is passed
203back to the client.
204\end{methoddesc}
205
Georg Brandlae91afd2007-04-01 22:39:10 +0000206\begin{methoddesc}[CGIXMLRPCRequestHandler]{register_introspection_functions}{}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000207Register the XML-RPC introspection functions
208\code{system.listMethods}, \code{system.methodHelp} and
209\code{system.methodSignature}.
210\end{methoddesc}
211
Georg Brandlae91afd2007-04-01 22:39:10 +0000212\begin{methoddesc}[CGIXMLRPCRequestHandler]{register_multicall_functions}{}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000213Register the XML-RPC multicall function \code{system.multicall}.
214\end{methoddesc}
215
Georg Brandlae91afd2007-04-01 22:39:10 +0000216\begin{methoddesc}[CGIXMLRPCRequestHandler]{handle_request}{\optional{request_text = None}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000217Handle a XML-RPC request. If \var{request_text} is given, it
218should be the POST data provided by the HTTP server,
219otherwise the contents of stdin will be used.
220\end{methoddesc}
221
222Example:
223
224\begin{verbatim}
225class MyFuncs:
Andrew M. Kuchling47a39b02005-12-04 17:17:46 +0000226 def div(self, x, y) : return x // y
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000227
228
229handler = CGIXMLRPCRequestHandler()
230handler.register_function(pow)
231handler.register_function(lambda x,y: x+y, 'add')
232handler.register_introspection_functions()
233handler.register_instance(MyFuncs())
234handler.handle_request()
Fred Drake42b567f2003-01-17 22:47:33 +0000235\end{verbatim}