下邊內(nèi)容是關(guān)于python rpc twisted 服務(wù)端和客戶端演示的內(nèi)容,希望能對小伙伴有些好處。
#服務(wù)器端代碼如下
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.echo("lala")
'lala'
>>> s.add(1, 2)
3
>>> s.fault()
Traceback (most recent call last):
...
xmlrpclib.Fault: <Fault 123: 'The fault procedure is faulty.'>
>>>