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