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 27 Aug 2003 06:17:49 -0000 @@ -24,49 +24,45 @@ 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 - -def main(): - from twisted.internet.app import Application - app = Application("xmlrpc") - r = Example() - app.listenTCP(7080, server.Site(r)) - return app - -application = main() - if __name__ == '__main__': - application.run(save=0) + from twisted.internet import reactor + r = Example() + reactor.listenTCP(7080, server.Site(r)) + reactor.run()
After we run this command, we can connect with a client and send commands @@ -86,6 +82,98 @@ xmlquote.rpy +
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() + +if __name__ == '__main__': + from twisted.internet import reactor + r = Example() + date = Date() + r.putSubHandler('date', date) + reactor.listenTCP(7080, server.Site(r)) + reactor.run() ++ +
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.
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." + +if __name__ == '__main__': + from twisted.internet import reactor + r = Example() + xmlrpc.addIntrospection(r) + reactor.listenTCP(7080, server.Site(r)) + reactor.run() ++ +
Note the method attributes help
and
+signature
which are used by the Introspection
+API methods system.methodHelp
and
+system.methodSignature
respectively. If no
+help
attribute is specified,
+the method's documentation string is used instead.
From the point of view, of a Twisted developer, there is little difference @@ -166,4 +254,3 @@