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