python rpc twisted 服務(wù)端和客戶端源碼演示

下邊內(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.'>

>>>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容