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