blob: dd486d9cc52d9bc58b0b2f06ffe63fe1c6bebe8e [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}
91class MyFuncs:
92 def div(self, x, y) : return div(x,y)
93
94
95server = SimpleXMLRPCServer(("localhost", 8000))
96server.register_function(pow)
97server.register_function(lambda x,y: x+y, 'add')
98server.register_introspection_functions()
99server.register_instance(MyFuncs())
100server.serve_forever()
101\end{verbatim}
102
103\subsection{CGIXMLRPCRequestHandler}
104
105The \class{CGIXMLRPCRequestHandler} class can be used to
106handle XML-RPC requests sent to Python CGI scripts.
107
108\begin{methoddesc}{register_function}{function\optional{, name}}
109Register a function that can respond to XML-RPC requests. If
Fred Drake42b567f2003-01-17 22:47:33 +0000110\var{name} is given, it will be the method name associated with
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000111function, otherwise \var{function.__name__} will be used. \var{name}
112can be either a normal or Unicode string, and may contain
113characters not legal in Python identifiers, including the period
114character.
115\end{methoddesc}
116
117\begin{methoddesc}{register_instance}{instance}
118Register an object which is used to expose method names
119which have not been registered using \method{register_function()}. If
120instance contains a \method{_dispatch()} method, it is called with the
121requested method name and the parameters from the
122request; the return value is returned to the client as the result.
Fred Drake42b567f2003-01-17 22:47:33 +0000123If instance does not have a \method{_dispatch()} method, it is searched
Martin v. Löwisd69663d2003-01-15 11:37:23 +0000124for an attribute matching the name of the requested method; if
125the requested method name contains periods, each
126component of the method name is searched for individually,
127with the effect that a simple hierarchical search is performed.
128The value found from this search is then called with the
129parameters from the request, and the return value is passed
130back to the client.
131\end{methoddesc}
132
133\begin{methoddesc}{register_introspection_functions}{}
134Register the XML-RPC introspection functions
135\code{system.listMethods}, \code{system.methodHelp} and
136\code{system.methodSignature}.
137\end{methoddesc}
138
139\begin{methoddesc}{register_multicall_functions}{}
140Register the XML-RPC multicall function \code{system.multicall}.
141\end{methoddesc}
142
143\begin{methoddesc}{handle_request}{\optional{request_text = None}}
144Handle a XML-RPC request. If \var{request_text} is given, it
145should be the POST data provided by the HTTP server,
146otherwise the contents of stdin will be used.
147\end{methoddesc}
148
149Example:
150
151\begin{verbatim}
152class MyFuncs:
153 def div(self, x, y) : return div(x,y)
154
155
156handler = CGIXMLRPCRequestHandler()
157handler.register_function(pow)
158handler.register_function(lambda x,y: x+y, 'add')
159handler.register_introspection_functions()
160handler.register_instance(MyFuncs())
161handler.handle_request()
Fred Drake42b567f2003-01-17 22:47:33 +0000162\end{verbatim}