Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 1 | \section{\module{SimpleXMLRPCServer} --- |
| 2 | Basic XML-RPC server} |
| 3 | |
| 4 | \declaremodule{standard}{SimpleXMLRPCServer} |
Fred Drake | 48704ee | 2001-11-28 07:32:53 +0000 | [diff] [blame] | 5 | \modulesynopsis{Basic XML-RPC server implementation.} |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 6 | \moduleauthor{Brian Quinlan}{brianq@activestate.com} |
| 7 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 8 | |
| 9 | |
| 10 | The \module{SimpleXMLRPCServer} module provides a basic server |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 11 | framework for XML-RPC servers written in Python. Servers can either |
| 12 | be free standing, using \class{SimpleXMLRPCServer}, or embedded in a |
| 13 | CGI environment, using \class{CGIXMLRPCRequestHandler}. |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 14 | |
| 15 | \begin{classdesc}{SimpleXMLRPCServer}{addr\optional{, |
Andrew M. Kuchling | 10a16de | 2005-12-04 16:34:40 +0000 | [diff] [blame] | 16 | requestHandler\optional{, |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame^] | 17 | logRequests\optional{allow_none\optional{, encoding}}}}} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame^] | 19 | Create a new server instance. This class |
| 20 | provides methods for registration of functions that can be called by |
| 21 | the XML-RPC protocol. The \var{requestHandler} parameter |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 22 | should be a factory for request handler instances; it defaults to |
| 23 | \class{SimpleXMLRPCRequestHandler}. The \var{addr} and |
| 24 | \var{requestHandler} parameters are passed to the |
| 25 | \class{\refmodule{SocketServer}.TCPServer} constructor. If |
| 26 | \var{logRequests} is true (the default), requests will be logged; |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame^] | 27 | setting this parameter to false will turn off logging. |
| 28 | The \var{allow_none} and \var{encoding} parameters are passed on to |
| 29 | \module{xmlrpclib} and control the XML-RPC responses that will be returned |
| 30 | from the server. |
| 31 | \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5} |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 32 | \end{classdesc} |
| 33 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame^] | 34 | \begin{classdesc}{CGIXMLRPCRequestHandler}{\optional{allow_none\optional{, encoding}}} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 35 | Create a new instance to handle XML-RPC requests in a CGI |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame^] | 36 | environment. |
| 37 | The \var{allow_none} and \var{encoding} parameters are passed on to |
| 38 | \module{xmlrpclib} and control the XML-RPC responses that will be returned |
| 39 | from the server. |
| 40 | \versionadded{2.3} |
| 41 | \versionchanged[The \var{allow_none} and \var{encoding} parameters were added]{2.5} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 42 | \end{classdesc} |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 43 | |
| 44 | \begin{classdesc}{SimpleXMLRPCRequestHandler}{} |
| 45 | Create a new request handler instance. This request handler |
| 46 | supports \code{POST} requests and modifies logging so that the |
| 47 | \var{logRequests} parameter to the \class{SimpleXMLRPCServer} |
| 48 | constructor parameter is honored. |
| 49 | \end{classdesc} |
| 50 | |
| 51 | |
| 52 | \subsection{SimpleXMLRPCServer Objects \label{simple-xmlrpc-servers}} |
| 53 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 54 | The \class{SimpleXMLRPCServer} class is based on |
| 55 | \class{SocketServer.TCPServer} and provides a means of creating |
| 56 | simple, stand alone XML-RPC servers. |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 57 | |
| 58 | \begin{methoddesc}[SimpleXMLRPCServer]{register_function}{function\optional{, |
| 59 | name}} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 60 | Register a function that can respond to XML-RPC requests. If |
| 61 | \var{name} is given, it will be the method name associated with |
| 62 | \var{function}, otherwise \code{\var{function}.__name__} will be |
| 63 | used. \var{name} can be either a normal or Unicode string, and may |
| 64 | contain characters not legal in Python identifiers, including the |
| 65 | period character. |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 66 | \end{methoddesc} |
| 67 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 68 | \begin{methoddesc}[SimpleXMLRPCServer]{register_instance}{instance\optional{, |
| 69 | allow_dotted_names}} |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 70 | Register an object which is used to expose method names which have |
| 71 | not been registered using \method{register_function()}. If |
| 72 | \var{instance} contains a \method{_dispatch()} method, it is called |
Skip Montanaro | 0bbf137 | 2004-09-03 00:04:05 +0000 | [diff] [blame] | 73 | with the requested method name and the parameters from the request. Its |
Walter Dörwald | cca3af3 | 2005-08-18 19:40:39 +0000 | [diff] [blame] | 74 | API is \code{def \method{_dispatch}(self, method, params)} (note that |
Skip Montanaro | 0bbf137 | 2004-09-03 00:04:05 +0000 | [diff] [blame] | 75 | \var{params} does not represent a variable argument list). If it calls an |
| 76 | underlying function to perform its task, that function is called as |
| 77 | \code{func(*params)}, expanding the parameter list. |
| 78 | The return value from \method{_dispatch()} is returned to the client as |
| 79 | the result. If |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 80 | \var{instance} does not have a \method{_dispatch()} method, it is |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 81 | searched for an attribute matching the name of the requested method. |
| 82 | |
| 83 | If the optional \var{allow_dotted_names} argument is true and the |
| 84 | instance does not have a \method{_dispatch()} method, then |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 85 | if the requested method name contains periods, each component of the |
| 86 | method name is searched for individually, with the effect that a |
| 87 | simple hierarchical search is performed. The value found from this |
| 88 | search is then called with the parameters from the request, and the |
| 89 | return value is passed back to the client. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 90 | |
| 91 | \begin{notice}[warning] |
| 92 | Enabling the \var{allow_dotted_names} option allows intruders to access |
| 93 | your module's global variables and may allow intruders to execute |
| 94 | arbitrary code on your machine. Only use this option on a secure, |
| 95 | closed network. |
| 96 | \end{notice} |
| 97 | |
| 98 | \versionchanged[\var{allow_dotted_names} was added to plug a security hole; |
| 99 | prior versions are insecure]{2.3.5, 2.4.1} |
| 100 | |
Fred Drake | e486e0d | 2001-09-28 22:02:21 +0000 | [diff] [blame] | 101 | \end{methoddesc} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 102 | |
| 103 | \begin{methoddesc}{register_introspection_functions}{} |
| 104 | Registers the XML-RPC introspection functions \code{system.listMethods}, |
| 105 | \code{system.methodHelp} and \code{system.methodSignature}. |
| 106 | \versionadded{2.3} |
| 107 | \end{methoddesc} |
| 108 | |
| 109 | \begin{methoddesc}{register_multicall_functions}{} |
| 110 | Registers the XML-RPC multicall function system.multicall. |
| 111 | \end{methoddesc} |
| 112 | |
| 113 | Example: |
| 114 | |
| 115 | \begin{verbatim} |
Andrew M. Kuchling | ab807e8 | 2004-12-01 18:34:11 +0000 | [diff] [blame] | 116 | from SimpleXMLRPCServer import SimpleXMLRPCServer |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 117 | |
Andrew M. Kuchling | ab807e8 | 2004-12-01 18:34:11 +0000 | [diff] [blame] | 118 | # Create server |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 119 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 120 | server.register_introspection_functions() |
Andrew M. Kuchling | ab807e8 | 2004-12-01 18:34:11 +0000 | [diff] [blame] | 121 | |
| 122 | # Register pow() function; this will use the value of |
| 123 | # pow.__name__ as the name, which is just 'pow'. |
| 124 | server.register_function(pow) |
| 125 | |
| 126 | # Register a function under a different name |
| 127 | def adder_function(x,y): |
| 128 | return x + y |
| 129 | server.register_function(adder_function, 'add') |
| 130 | |
| 131 | # Register an instance; all the methods of the instance are |
| 132 | # published as XML-RPC methods (in this case, just 'div'). |
| 133 | class MyFuncs: |
| 134 | def div(self, x, y): |
| 135 | return x // y |
| 136 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 137 | server.register_instance(MyFuncs()) |
Andrew M. Kuchling | ab807e8 | 2004-12-01 18:34:11 +0000 | [diff] [blame] | 138 | |
| 139 | # Run the server's main loop |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 140 | server.serve_forever() |
| 141 | \end{verbatim} |
| 142 | |
Andrew M. Kuchling | ab807e8 | 2004-12-01 18:34:11 +0000 | [diff] [blame] | 143 | The following client code will call the methods made available by |
| 144 | the preceding server: |
| 145 | |
| 146 | \begin{verbatim} |
| 147 | import xmlrpclib |
| 148 | |
| 149 | s = xmlrpclib.Server('http://localhost:8000') |
| 150 | print s.pow(2,3) # Returns 2**3 = 8 |
| 151 | print s.add(2,3) # Returns 5 |
| 152 | print s.div(5,2) # Returns 5//2 = 2 |
| 153 | |
| 154 | # Print list of available methods |
| 155 | print s.system.listMethods() |
| 156 | \end{verbatim} |
| 157 | |
| 158 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 159 | \subsection{CGIXMLRPCRequestHandler} |
| 160 | |
| 161 | The \class{CGIXMLRPCRequestHandler} class can be used to |
| 162 | handle XML-RPC requests sent to Python CGI scripts. |
| 163 | |
| 164 | \begin{methoddesc}{register_function}{function\optional{, name}} |
| 165 | Register a function that can respond to XML-RPC requests. If |
Fred Drake | 42b567f | 2003-01-17 22:47:33 +0000 | [diff] [blame] | 166 | \var{name} is given, it will be the method name associated with |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 167 | function, otherwise \var{function.__name__} will be used. \var{name} |
| 168 | can be either a normal or Unicode string, and may contain |
| 169 | characters not legal in Python identifiers, including the period |
| 170 | character. |
| 171 | \end{methoddesc} |
| 172 | |
| 173 | \begin{methoddesc}{register_instance}{instance} |
| 174 | Register an object which is used to expose method names |
| 175 | which have not been registered using \method{register_function()}. If |
| 176 | instance contains a \method{_dispatch()} method, it is called with the |
| 177 | requested method name and the parameters from the |
| 178 | request; the return value is returned to the client as the result. |
Fred Drake | 42b567f | 2003-01-17 22:47:33 +0000 | [diff] [blame] | 179 | If instance does not have a \method{_dispatch()} method, it is searched |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 180 | for an attribute matching the name of the requested method; if |
| 181 | the requested method name contains periods, each |
| 182 | component of the method name is searched for individually, |
| 183 | with the effect that a simple hierarchical search is performed. |
| 184 | The value found from this search is then called with the |
| 185 | parameters from the request, and the return value is passed |
| 186 | back to the client. |
| 187 | \end{methoddesc} |
| 188 | |
| 189 | \begin{methoddesc}{register_introspection_functions}{} |
| 190 | Register the XML-RPC introspection functions |
| 191 | \code{system.listMethods}, \code{system.methodHelp} and |
| 192 | \code{system.methodSignature}. |
| 193 | \end{methoddesc} |
| 194 | |
| 195 | \begin{methoddesc}{register_multicall_functions}{} |
| 196 | Register the XML-RPC multicall function \code{system.multicall}. |
| 197 | \end{methoddesc} |
| 198 | |
| 199 | \begin{methoddesc}{handle_request}{\optional{request_text = None}} |
| 200 | Handle a XML-RPC request. If \var{request_text} is given, it |
| 201 | should be the POST data provided by the HTTP server, |
| 202 | otherwise the contents of stdin will be used. |
| 203 | \end{methoddesc} |
| 204 | |
| 205 | Example: |
| 206 | |
| 207 | \begin{verbatim} |
| 208 | class MyFuncs: |
| 209 | def div(self, x, y) : return div(x,y) |
| 210 | |
| 211 | |
| 212 | handler = CGIXMLRPCRequestHandler() |
| 213 | handler.register_function(pow) |
| 214 | handler.register_function(lambda x,y: x+y, 'add') |
| 215 | handler.register_introspection_functions() |
| 216 | handler.register_instance(MyFuncs()) |
| 217 | handler.handle_request() |
Fred Drake | 42b567f | 2003-01-17 22:47:33 +0000 | [diff] [blame] | 218 | \end{verbatim} |