blob: 0170c1a431ec201d9c8dee0a94f404e019ecc9eb [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
58\begin{methoddesc}[SimpleXMLRPCServer]{register_instance}{instance}
59 Register an object which is used to expose method names which have
60 not been registered using \method{register_function()}. If
61 \var{instance} contains a \method{_dispatch()} method, it is called
Skip Montanaro0bbf1372004-09-03 00:04:05 +000062 with the requested method name and the parameters from the request. Its
63 API is \code{def \method{_dispatch}(self, method, params)} (note tha
64 \var{params} does not represent a variable argument list). If it calls an
65 underlying function to perform its task, that function is called as
66 \code{func(*params)}, expanding the parameter list.
67 The return value from \method{_dispatch()} is returned to the client as
68 the result. If
Fred Drakee486e0d2001-09-28 22:02:21 +000069 \var{instance} does not have a \method{_dispatch()} method, it is
70 searched for an attribute matching the name of the requested method;
71 if the requested method name contains periods, each component of the
72 method name is searched for individually, with the effect that a
73 simple hierarchical search is performed. The value found from this
74 search is then called with the parameters from the request, and the
75 return value is passed back to the client.
76\end{methoddesc}
Martin v. Löwisd69663d2003-01-15 11:37:23 +000077
78\begin{methoddesc}{register_introspection_functions}{}
79 Registers the XML-RPC introspection functions \code{system.listMethods},
80 \code{system.methodHelp} and \code{system.methodSignature}.
81 \versionadded{2.3}
82\end{methoddesc}
83
84\begin{methoddesc}{register_multicall_functions}{}
85 Registers the XML-RPC multicall function system.multicall.
86\end{methoddesc}
87
88Example:
89
90\begin{verbatim}
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +000091from SimpleXMLRPCServer import SimpleXMLRPCServer
Martin v. Löwisd69663d2003-01-15 11:37:23 +000092
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +000093# Create server
Martin v. Löwisd69663d2003-01-15 11:37:23 +000094server = SimpleXMLRPCServer(("localhost", 8000))
Martin v. Löwisd69663d2003-01-15 11:37:23 +000095server.register_introspection_functions()
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +000096
97# Register pow() function; this will use the value of
98# pow.__name__ as the name, which is just 'pow'.
99server.register_function(pow)
100
101# Register a function under a different name
102def adder_function(x,y):
103 return x + y
104server.register_function(adder_function, 'add')
105
106# Register an instance; all the methods of the instance are
107# published as XML-RPC methods (in this case, just 'div').
108class MyFuncs:
109 def div(self, x, y):
110 return x // y
111
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000112server.register_instance(MyFuncs())
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000113
114# Run the server's main loop
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000115server.serve_forever()
116\end{verbatim}
117
Andrew M. Kuchlingab807e82004-12-01 18:34:11 +0000118The following client code will call the methods made available by
119the preceding server:
120
121\begin{verbatim}
122import xmlrpclib
123
124s = xmlrpclib.Server('http://localhost:8000')
125print s.pow(2,3) # Returns 2**3 = 8
126print s.add(2,3) # Returns 5
127print s.div(5,2) # Returns 5//2 = 2
128
129# Print list of available methods
130print s.system.listMethods()
131\end{verbatim}
132
133
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000134\subsection{CGIXMLRPCRequestHandler}
135
136The \class{CGIXMLRPCRequestHandler} class can be used to
137handle XML-RPC requests sent to Python CGI scripts.
138
139\begin{methoddesc}{register_function}{function\optional{, name}}
140Register a function that can respond to XML-RPC requests. If
Fred Drake42b567f2003-01-17 22:47:33 +0000141\var{name} is given, it will be the method name associated with
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000142function, otherwise \var{function.__name__} will be used. \var{name}
143can be either a normal or Unicode string, and may contain
144characters not legal in Python identifiers, including the period
145character.
146\end{methoddesc}
147
148\begin{methoddesc}{register_instance}{instance}
149Register an object which is used to expose method names
150which have not been registered using \method{register_function()}. If
151instance contains a \method{_dispatch()} method, it is called with the
152requested method name and the parameters from the
153request; the return value is returned to the client as the result.
Fred Drake42b567f2003-01-17 22:47:33 +0000154If instance does not have a \method{_dispatch()} method, it is searched
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000155for an attribute matching the name of the requested method; if
156the requested method name contains periods, each
157component of the method name is searched for individually,
158with the effect that a simple hierarchical search is performed.
159The value found from this search is then called with the
160parameters from the request, and the return value is passed
161back to the client.
162\end{methoddesc}
163
164\begin{methoddesc}{register_introspection_functions}{}
165Register the XML-RPC introspection functions
166\code{system.listMethods}, \code{system.methodHelp} and
167\code{system.methodSignature}.
168\end{methoddesc}
169
170\begin{methoddesc}{register_multicall_functions}{}
171Register the XML-RPC multicall function \code{system.multicall}.
172\end{methoddesc}
173
174\begin{methoddesc}{handle_request}{\optional{request_text = None}}
175Handle a XML-RPC request. If \var{request_text} is given, it
176should be the POST data provided by the HTTP server,
177otherwise the contents of stdin will be used.
178\end{methoddesc}
179
180Example:
181
182\begin{verbatim}
183class MyFuncs:
184 def div(self, x, y) : return div(x,y)
185
186
187handler = CGIXMLRPCRequestHandler()
188handler.register_function(pow)
189handler.register_function(lambda x,y: x+y, 'add')
190handler.register_introspection_functions()
191handler.register_instance(MyFuncs())
192handler.handle_request()
Fred Drake42b567f2003-01-17 22:47:33 +0000193\end{verbatim}