python rpc twisted 服务端和客户端代码演示
#服务器端代码如下 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 xmlrpc_fault(self): """ Raise a Fault indicating that the procedure should not be used. """ raise xmlrpc.Fault(123, "The fault procedure is faulty.") if __name__ == '__main__': from twisted.internet import reactor r = Example() reactor.listenTCP(7080, server.Site(r)) reactor.run()
>>> import xmlrpclib >>> s = xmlrpclib.Server('http://localhost:7080/') >>> s.echo("lala") 'lala' >>> s.add(1, 2) 3 >>> s.fault() Traceback (most recent call last): ... xmlrpclib.Fault: >>>