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