blob: ad8ccd483b17b97bc23fa14ff6cf40ab1a0bfa71 [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
9
10The \module{SimpleXMLRPCServer} module provides a basic server
Martin v. Löwisd69663d2003-01-15 11:37:23 +000011framework for XML-RPC servers written in Python. Servers can either
12be free standing, using \class{SimpleXMLRPCServer}, or embedded in a
13CGI environment, using \class{CGIXMLRPCRequestHandler}.
Fred Drakee486e0d2001-09-28 22:02:21 +000014
15\begin{classdesc}{SimpleXMLRPCServer}{addr\optional{,
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +000016 requestHandler\optional{,
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000017 logRequests\optional{allow_none\optional{, encoding}}}}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000018
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000019 Create a new server instance. This class
20 provides methods for registration of functions that can be called by
21 the XML-RPC protocol. The \var{requestHandler} parameter
Fred Drakee486e0d2001-09-28 22:02:21 +000022 should be a factory for request handler instances; it defaults to
23 \class{SimpleXMLRPCRequestHandler}. The \var{addr} and
24 \var{requestHandler} parameters are passed to the
25 \class{\refmodule{SocketServer}.TCPServer} constructor. If
26 \var{logRequests} is true (the default), requests will be logged;
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000027 setting this parameter to false will turn off logging.
28 The \var{allow_none} and \var{encoding} parameters are passed on to
29 \module{xmlrpclib} and control the XML-RPC responses that will be returned
30 from the server.
31 \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5}
Fred Drakee486e0d2001-09-28 22:02:21 +000032\end{classdesc}
33
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000034\begin{classdesc}{CGIXMLRPCRequestHandler}{\optional{allow_none\optional{, encoding}}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000035 Create a new instance to handle XML-RPC requests in a CGI
Andrew M. Kuchling427aedb2005-12-04 17:13:12 +000036 environment.
37 The \var{allow_none} and \var{encoding} parameters are passed on to
38 \module{xmlrpclib} and control the XML-RPC responses that will be returned
39 from the server.
40 \versionadded{2.3}
41 \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000042\end{classdesc}
Fred Drakee486e0d2001-09-28 22:02:21 +000043
44\begin{classdesc}{SimpleXMLRPCRequestHandler}{}
45 Create a new request handler instance. This request handler
46 supports \code{POST} requests and modifies logging so that the
47 \var{logRequests} parameter to the \class{SimpleXMLRPCServer}
48 constructor parameter is honored.
49\end{classdesc}
50
51
52\subsection{SimpleXMLRPCServer Objects \label{simple-xmlrpc-servers}}
53
Martin v. Löwisd69663d2003-01-15 11:37:23 +000054The \class{SimpleXMLRPCServer} class is based on
55\class{SocketServer.TCPServer} and provides a means of creating
56simple, stand alone XML-RPC servers.
Fred Drakee486e0d2001-09-28 22:02:21 +000057
58\begin{methoddesc}[SimpleXMLRPCServer]{register_function}{function\optional{,
59 name}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000060 Register a function that can respond to XML-RPC requests. If
61 \var{name} is given, it will be the method name associated with
62 \var{function}, otherwise \code{\var{function}.__name__} will be
63 used. \var{name} can be either a normal or Unicode string, and may
64 contain characters not legal in Python identifiers, including the
65 period character.
Fred Drakee486e0d2001-09-28 22:02:21 +000066\end{methoddesc}
67
Guido van Rossumd0641422005-02-03 15:01:24 +000068\begin{methoddesc}[SimpleXMLRPCServer]{register_instance}{instance\optional{,
69 allow_dotted_names}}
Fred Drakee486e0d2001-09-28 22:02:21 +000070 Register an object which is used to expose method names which have
71 not been registered using \method{register_function()}. If
72 \var{instance} contains a \method{_dispatch()} method, it is called
Skip Montanaro0bbf1372004-09-03 00:04:05 +000073 with the requested method name and the parameters from the request. Its
Walter Dörwaldcca3af32005-08-18 19:40:39 +000074 API is \code{def \method{_dispatch}(self, method, params)} (note that
Skip Montanaro0bbf1372004-09-03 00:04:05 +000075 \var{params} does not represent a variable argument list). If it calls an
76 underlying function to perform its task, that function is called as
77 \code{func(*params)}, expanding the parameter list.
78 The return value from \method{_dispatch()} is returned to the client as
79 the result. If
Fred Drakee486e0d2001-09-28 22:02:21 +000080 \var{instance} does not have a \method{_dispatch()} method, it is
Guido van Rossumd0641422005-02-03 15:01:24 +000081 searched for an attribute matching the name of the requested method.
82
83 If the optional \var{allow_dotted_names} argument is true and the
84 instance does not have a \method{_dispatch()} method, then
Fred Drakee486e0d2001-09-28 22:02:21 +000085 if the requested method name contains periods, each component of the
86 method name is searched for individually, with the effect that a
87 simple hierarchical search is performed. The value found from this
88 search is then called with the parameters from the request, and the
89 return value is passed back to the client.
Guido van Rossumd0641422005-02-03 15:01:24 +000090
91 \begin{notice}[warning]
92 Enabling the \var{allow_dotted_names} option allows intruders to access
93 your module's global variables and may allow intruders to execute
94 arbitrary code on your machine. Only use this option on a secure,
95 closed network.
96 \end{notice}
97
98 \versionchanged[\var{allow_dotted_names} was added to plug a security hole;
99 prior versions are insecure]{2.3.5, 2.4.1}
100
Fred Drakee486e0d2001-09-28 22:02:21 +0000101\end{methoddesc}
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000102
103\begin{methoddesc}{register_introspection_functions}{}
104 Registers the XML-RPC introspection functions \code{system.listMethods},
105 \code{system.methodHelp} and \code{system.methodSignature}.
106 \versionadded{2.3}
107\end{methoddesc}
108
109\begin{methoddesc}{register_multicall_functions}{}
110 Registers the XML-RPC multicall function system.multicall.
111\end{methoddesc}
112
113Example:
114
115\begin{verbatim}
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000116from SimpleXMLRPCServer import SimpleXMLRPCServer
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000117
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000118# Create server
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000119server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000120server.register_introspection_functions()
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000121
122# Register pow() function; this will use the value of
123# pow.__name__ as the name, which is just 'pow'.
124server.register_function(pow)
125
126# Register a function under a different name
127def adder_function(x,y):
128 return x + y
129server.register_function(adder_function, 'add')
130
131# Register an instance; all the methods of the instance are
132# published as XML-RPC methods (in this case, just 'div').
133class MyFuncs:
134 def div(self, x, y):
135 return x // y
136
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000137server.register_instance(MyFuncs())
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000138
139# Run the server's main loop
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000140server.serve_forever()
141\end{verbatim}
142
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000143The following client code will call the methods made available by
144the preceding server:
145
146\begin{verbatim}
147import xmlrpclib
148
149s = xmlrpclib.Server('http://localhost:8000')
150print s.pow(2,3) # Returns 2**3 = 8
151print s.add(2,3) # Returns 5
152print s.div(5,2) # Returns 5//2 = 2
153
154# Print list of available methods
155print s.system.listMethods()
156\end{verbatim}
157
158
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000159\subsection{CGIXMLRPCRequestHandler}
160
161The \class{CGIXMLRPCRequestHandler} class can be used to
162handle XML-RPC requests sent to Python CGI scripts.
163
164\begin{methoddesc}{register_function}{function\optional{, name}}
165Register a function that can respond to XML-RPC requests. If
Fred Drake42b567f2003-01-17 22:47:33 +0000166\var{name} is given, it will be the method name associated with
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000167function, otherwise \var{function.__name__} will be used. \var{name}
168can be either a normal or Unicode string, and may contain
169characters not legal in Python identifiers, including the period
170character.
171\end{methoddesc}
172
173\begin{methoddesc}{register_instance}{instance}
174Register an object which is used to expose method names
175which have not been registered using \method{register_function()}. If
176instance contains a \method{_dispatch()} method, it is called with the
177requested method name and the parameters from the
178request; the return value is returned to the client as the result.
Fred Drake42b567f2003-01-17 22:47:33 +0000179If instance does not have a \method{_dispatch()} method, it is searched
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000180for an attribute matching the name of the requested method; if
181the requested method name contains periods, each
182component of the method name is searched for individually,
183with the effect that a simple hierarchical search is performed.
184The value found from this search is then called with the
185parameters from the request, and the return value is passed
186back to the client.
187\end{methoddesc}
188
189\begin{methoddesc}{register_introspection_functions}{}
190Register the XML-RPC introspection functions
191\code{system.listMethods}, \code{system.methodHelp} and
192\code{system.methodSignature}.
193\end{methoddesc}
194
195\begin{methoddesc}{register_multicall_functions}{}
196Register the XML-RPC multicall function \code{system.multicall}.
197\end{methoddesc}
198
199\begin{methoddesc}{handle_request}{\optional{request_text = None}}
200Handle a XML-RPC request. If \var{request_text} is given, it
201should be the POST data provided by the HTTP server,
202otherwise the contents of stdin will be used.
203\end{methoddesc}
204
205Example:
206
207\begin{verbatim}
208class MyFuncs:
209 def div(self, x, y) : return div(x,y)
210
211
212handler = CGIXMLRPCRequestHandler()
213handler.register_function(pow)
214handler.register_function(lambda x,y: x+y, 'add')
215handler.register_introspection_functions()
216handler.register_instance(MyFuncs())
217handler.handle_request()
Fred Drake42b567f2003-01-17 22:47:33 +0000218\end{verbatim}