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