| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
An example of an XML-RPC server in Twisted. |
| 6 |
|
| 7 |
Usage: |
| 8 |
$ python xmlrpc.py |
| 9 |
|
| 10 |
An example session (assuming the server is running): |
| 11 |
|
| 12 |
>>> import xmlrpclib |
| 13 |
>>> s = xmlrpclib.Server('http://localhost:7080/') |
| 14 |
>>> s.echo("lala") |
| 15 |
['lala'] |
| 16 |
>>> s.echo("lala", 1) |
| 17 |
['lala', 1] |
| 18 |
>>> s.echo("lala", 4) |
| 19 |
['lala', 4] |
| 20 |
>>> s.echo("lala", 4, 3.4) |
| 21 |
['lala', 4, 3.3999999999999999] |
| 22 |
>>> s.echo("lala", 4, [1, 2]) |
| 23 |
['lala', 4, [1, 2]] |
| 24 |
|
| 25 |
""" |
| 26 |
|
| 27 |
from xmlrpc.client import Fault |
| 28 |
|
| 29 |
from twisted.internet import defer |
| 30 |
from twisted.web import xmlrpc |
| 31 |
|
| 32 |
|
| 33 |
class Echoer(xmlrpc.XMLRPC): |
| 34 |
""" |
| 35 |
An example object to be published. |
| 36 |
|
| 37 |
Has five methods accessible by XML-RPC, 'echo', 'hello', 'defer', |
| 38 |
'defer_fail' and 'fail. |
| 39 |
""" |
| 40 |
|
| 41 |
def xmlrpc_echo(self, *args): |
| 42 |
""" |
| 43 |
Return all passed args. |
| 44 |
""" |
| 45 |
return args |
| 46 |
|
| 47 |
def xmlrpc_hello(self): |
| 48 |
""" |
| 49 |
Return 'hello, world'. |
| 50 |
""" |
| 51 |
return "hello, world!" |
| 52 |
|
| 53 |
def xmlrpc_defer(self): |
| 54 |
""" |
| 55 |
Show how xmlrpc methods can return Deferred. |
| 56 |
""" |
| 57 |
return defer.succeed("hello") |
| 58 |
|
| 59 |
def xmlrpc_defer_fail(self): |
| 60 |
""" |
| 61 |
Show how xmlrpc methods can return failed Deferred. |
| 62 |
""" |
| 63 |
return defer.fail(12) |
| 64 |
|
| 65 |
def xmlrpc_fail(self): |
| 66 |
""" |
| 67 |
Show how we can return a failure code. |
| 68 |
""" |
| 69 |
return Fault(7, "Out of cheese.") |
| 70 |
|
| 71 |
|
| 72 |
def main(): |
| 73 |
from twisted.internet import reactor |
| 74 |
from twisted.web import server |
| 75 |
|
| 76 |
r = Echoer() |
| 77 |
reactor.listenTCP(7080, server.Site(r)) |
| 78 |
reactor.run() |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
main() |