blob: 4d179f6c4e24bcef18922f1f087c81361d8dd260 [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{,
17 logRequests\optional{allow_none}}}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000018
Fred Drakee486e0d2001-09-28 22:02:21 +000019 Create a new server instance. The \var{requestHandler} parameter
20 should be a factory for request handler instances; it defaults to
21 \class{SimpleXMLRPCRequestHandler}. The \var{addr} and
22 \var{requestHandler} parameters are passed to the
23 \class{\refmodule{SocketServer}.TCPServer} constructor. If
24 \var{logRequests} is true (the default), requests will be logged;
25 setting this parameter to false will turn off logging. This class
26 provides methods for registration of functions that can be called by
27 the XML-RPC protocol.
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +000028 \versionchanged[The \var{allow_none} parameter was added]{2.5}
Fred Drakee486e0d2001-09-28 22:02:21 +000029\end{classdesc}
30
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +000031\begin{classdesc}{CGIXMLRPCRequestHandler}{\optional{allow_none}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000032 Create a new instance to handle XML-RPC requests in a CGI
33 environment. \versionadded{2.3}
Andrew M. Kuchling10a16de2005-12-04 16:34:40 +000034 \versionchanged[The \var{allow_none} parameter was added]{2.5}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000035\end{classdesc}
Fred Drakee486e0d2001-09-28 22:02:21 +000036
37\begin{classdesc}{SimpleXMLRPCRequestHandler}{}
38 Create a new request handler instance. This request handler
39 supports \code{POST} requests and modifies logging so that the
40 \var{logRequests} parameter to the \class{SimpleXMLRPCServer}
41 constructor parameter is honored.
42\end{classdesc}
43
44
45\subsection{SimpleXMLRPCServer Objects \label{simple-xmlrpc-servers}}
46
Martin v. Löwisd69663d2003-01-15 11:37:23 +000047The \class{SimpleXMLRPCServer} class is based on
48\class{SocketServer.TCPServer} and provides a means of creating
49simple, stand alone XML-RPC servers.
Fred Drakee486e0d2001-09-28 22:02:21 +000050
51\begin{methoddesc}[SimpleXMLRPCServer]{register_function}{function\optional{,
52 name}}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000053 Register a function that can respond to XML-RPC requests. If
54 \var{name} is given, it will be the method name associated with
55 \var{function}, otherwise \code{\var{function}.__name__} will be
56 used. \var{name} can be either a normal or Unicode string, and may
57 contain characters not legal in Python identifiers, including the
58 period character.
Fred Drakee486e0d2001-09-28 22:02:21 +000059\end{methoddesc}
60
Guido van Rossumd0641422005-02-03 15:01:24 +000061\begin{methoddesc}[SimpleXMLRPCServer]{register_instance}{instance\optional{,
62 allow_dotted_names}}
Fred Drakee486e0d2001-09-28 22:02:21 +000063 Register an object which is used to expose method names which have
64 not been registered using \method{register_function()}. If
65 \var{instance} contains a \method{_dispatch()} method, it is called
Skip Montanaro0bbf1372004-09-03 00:04:05 +000066 with the requested method name and the parameters from the request. Its
Walter Dörwaldcca3af32005-08-18 19:40:39 +000067 API is \code{def \method{_dispatch}(self, method, params)} (note that
Skip Montanaro0bbf1372004-09-03 00:04:05 +000068 \var{params} does not represent a variable argument list). If it calls an
69 underlying function to perform its task, that function is called as
70 \code{func(*params)}, expanding the parameter list.
71 The return value from \method{_dispatch()} is returned to the client as
72 the result. If
Fred Drakee486e0d2001-09-28 22:02:21 +000073 \var{instance} does not have a \method{_dispatch()} method, it is
Guido van Rossumd0641422005-02-03 15:01:24 +000074 searched for an attribute matching the name of the requested method.
75
76 If the optional \var{allow_dotted_names} argument is true and the
77 instance does not have a \method{_dispatch()} method, then
Fred Drakee486e0d2001-09-28 22:02:21 +000078 if the requested method name contains periods, each component of the
79 method name is searched for individually, with the effect that a
80 simple hierarchical search is performed. The value found from this
81 search is then called with the parameters from the request, and the
82 return value is passed back to the client.
Guido van Rossumd0641422005-02-03 15:01:24 +000083
84 \begin{notice}[warning]
85 Enabling the \var{allow_dotted_names} option allows intruders to access
86 your module's global variables and may allow intruders to execute
87 arbitrary code on your machine. Only use this option on a secure,
88 closed network.
89 \end{notice}
90
91 \versionchanged[\var{allow_dotted_names} was added to plug a security hole;
92 prior versions are insecure]{2.3.5, 2.4.1}
93
Fred Drakee486e0d2001-09-28 22:02:21 +000094\end{methoddesc}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000095
96\begin{methoddesc}{register_introspection_functions}{}
97 Registers the XML-RPC introspection functions \code{system.listMethods},
98 \code{system.methodHelp} and \code{system.methodSignature}.
99 \versionadded{2.3}
100\end{methoddesc}
101
102\begin{methoddesc}{register_multicall_functions}{}
103 Registers the XML-RPC multicall function system.multicall.
104\end{methoddesc}
105
106Example:
107
108\begin{verbatim}
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000109from SimpleXMLRPCServer import SimpleXMLRPCServer
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000110
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000111# Create server
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000112server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000113server.register_introspection_functions()
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000114
115# Register pow() function; this will use the value of
116# pow.__name__ as the name, which is just 'pow'.
117server.register_function(pow)
118
119# Register a function under a different name
120def adder_function(x,y):
121 return x + y
122server.register_function(adder_function, 'add')
123
124# Register an instance; all the methods of the instance are
125# published as XML-RPC methods (in this case, just 'div').
126class MyFuncs:
127 def div(self, x, y):
128 return x // y
129
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000130server.register_instance(MyFuncs())
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000131
132# Run the server's main loop
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000133server.serve_forever()
134\end{verbatim}
135
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000136The following client code will call the methods made available by
137the preceding server:
138
139\begin{verbatim}
140import xmlrpclib
141
142s = xmlrpclib.Server('http://localhost:8000')
143print s.pow(2,3) # Returns 2**3 = 8
144print s.add(2,3) # Returns 5
145print s.div(5,2) # Returns 5//2 = 2
146
147# Print list of available methods
148print s.system.listMethods()
149\end{verbatim}
150
151
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000152\subsection{CGIXMLRPCRequestHandler}
153
154The \class{CGIXMLRPCRequestHandler} class can be used to
155handle XML-RPC requests sent to Python CGI scripts.
156
157\begin{methoddesc}{register_function}{function\optional{, name}}
158Register a function that can respond to XML-RPC requests. If
Fred Drake42b567f2003-01-17 22:47:33 +0000159\var{name} is given, it will be the method name associated with
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000160function, otherwise \var{function.__name__} will be used. \var{name}
161can be either a normal or Unicode string, and may contain
162characters not legal in Python identifiers, including the period
163character.
164\end{methoddesc}
165
166\begin{methoddesc}{register_instance}{instance}
167Register an object which is used to expose method names
168which have not been registered using \method{register_function()}. If
169instance contains a \method{_dispatch()} method, it is called with the
170requested method name and the parameters from the
171request; the return value is returned to the client as the result.
Fred Drake42b567f2003-01-17 22:47:33 +0000172If instance does not have a \method{_dispatch()} method, it is searched
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000173for an attribute matching the name of the requested method; if
174the requested method name contains periods, each
175component of the method name is searched for individually,
176with the effect that a simple hierarchical search is performed.
177The value found from this search is then called with the
178parameters from the request, and the return value is passed
179back to the client.
180\end{methoddesc}
181
182\begin{methoddesc}{register_introspection_functions}{}
183Register the XML-RPC introspection functions
184\code{system.listMethods}, \code{system.methodHelp} and
185\code{system.methodSignature}.
186\end{methoddesc}
187
188\begin{methoddesc}{register_multicall_functions}{}
189Register the XML-RPC multicall function \code{system.multicall}.
190\end{methoddesc}
191
192\begin{methoddesc}{handle_request}{\optional{request_text = None}}
193Handle a XML-RPC request. If \var{request_text} is given, it
194should be the POST data provided by the HTTP server,
195otherwise the contents of stdin will be used.
196\end{methoddesc}
197
198Example:
199
200\begin{verbatim}
201class MyFuncs:
202 def div(self, x, y) : return div(x,y)
203
204
205handler = CGIXMLRPCRequestHandler()
206handler.register_function(pow)
207handler.register_function(lambda x,y: x+y, 'add')
208handler.register_introspection_functions()
209handler.register_instance(MyFuncs())
210handler.handle_request()
Fred Drake42b567f2003-01-17 22:47:33 +0000211\end{verbatim}