Index: doc/howto/xmlrpc.html =================================================================== RCS file: /cvs/Twisted/doc/howto/xmlrpc.html,v retrieving revision 1.11 diff -u -r1.11 xmlrpc.html --- doc/howto/xmlrpc.html 19 Jul 2003 12:42:34 -0000 1.11 +++ doc/howto/xmlrpc.html 26 Aug 2003 22:44:24 -0000 @@ -24,33 +24,36 @@ arguments determine what arguments it will accept from XML-RPC clients. The result is what will be returned to the clients.
-Methods published via XML-RPC can return all the basic XML-RPC types,
-such as strings, lists and so on (just return a regular python integer, etc).
-They can also return Failure instances to indicate
-an error has occurred, or Binary
,
-Boolean
or
-DateTime
instances (all of these
-are the same as the respective classes in xmlrpclib. In addition, XML-RPC
-published methods can return Deferred
-instances whose results are one of
-the above. This allows you to return results that can't be calculated immediately,
-such as database queries. See the Deferred documentation for more details.
XMLRPC
instances are Resource objects, and they can thus be published using
-a Site. The following example has two methods published via XML-RPC,
-add(a, b)
and echo(x)
. You can run it directly
-or with twistd -y script.py
Methods published via XML-RPC can return all the basic XML-RPC
+types, such as strings, lists and so on (just return a regular python
+integer, etc). They can also return Failure instances to indicate an
+error has occurred, or Binary
, Boolean
or DateTime
instances (all of these are the same as
+the respective classes in xmlrpclib. In addition, XML-RPC published
+methods can return Deferred
instances whose results are one of the
+above. This allows you to return results that can't be calculated
+immediately, such as database queries. See the Deferred documentation for more details.
XMLRPC
instances
+are Resource objects, and they can thus be published using a Site. The
+following example has two methods published via XML-RPC, add(a,
+b)
and echo(x)
. You can run it directly or with
+twistd -y script.py
from twisted.web import xmlrpc, server class Example(xmlrpc.XMLRPC): """An example object to be published.""" - + def xmlrpc_echo(self, x): """Return all passed args.""" return x - + def xmlrpc_add(self, a, b): """Return sum of arguments.""" return a + b @@ -86,6 +89,112 @@ xmlquote.rpy +Using XML-RPC sub-handlers
+ +XML-RPC resource can be nested so that one handler calls another if +a method with a given prefix is called. For example, to add support for +an XML-RPC method
+ +date.time()
to the +Example
class, you could do the following:+ +import time +from twisted.web import xmlrpc, server + +class Example(xmlrpc.XMLRPC): + """An example object to be published.""" + + def xmlrpc_echo(self, x): + """Return all passed args.""" + return x + + def xmlrpc_add(self, a, b): + """Return sum of arguments.""" + return a + b + +class Date(xmlrpc.XMLRPC): + """Serve the XML-RPC 'time' method.""" + + def xmlrpc_time(self): + """Return UNIX time.""" + return time.time() + +def main(): + from twisted.internet.app import Application + app = Application("xmlrpc") + r = Example() + date = Date() + r.putSubHandler('date', date) + app.listenTCP(7080, server.Site(r)) + return app + +application = main() + +if __name__ == '__main__': + application.run(save=0) ++ +By default, a period ('.') separates the prefix from the method +name, but you can use a different character by overriding the
+ +XMLRPC.separator
data member in your base +XML-RPC server. XML-RPC servers may be nested to arbitrary depths +using this method.Adding XML-RPC Introspection support
+ +XML-RPC has an informal Introspection API that specifies three +methods in a
+ +system
sub-handler which allow a client to query +a server about the server's API. Adding Introspection support to the +Example
class is easy using the +XMLRPCIntrospection
+class:+ +from twisted.web import xmlrpc, server + +class Example(xmlrpc.XMLRPC): + """An example object to be published.""" + + def xmlrpc_echo(self, x): + """Return all passed args.""" + return x + + xmlrpc_echo.signature = [['string', 'string'], + ['int', 'int'], + ['double', 'double'], + ['array', 'array'], + ['struct', 'struct']] + + def xmlrpc_add(self, a, b): + """Return sum of arguments.""" + return a + b + + xmlrpc_add.signature = [['int', 'int', 'int'], + ['double', 'double', 'double']] + xmlrpc_add.help = "Add the arguments and return the sum." + +def main(): + from twisted.internet.app import Application + app = Application("xmlrpc") + r = Example() + xmlrpc.XMLRPCIntrospection(r) + app.listenTCP(7080, server.Site(r)) + return app + +application = main() + +if __name__ == '__main__': + application.run(save=0) ++ +Note the method attributes
+help
and +signature
which are used by the Introspection +API methodssystem.methodHelp
and +system.methodSignature
respectively. If no +help
attribute is specified, +the method's documentation string is used instead.SOAP Support
From the point of view, of a Twisted developer, there is little difference @@ -166,4 +275,3 @@