1. SOAP

from twisted.web import soap,server
class Quoter(soap.SOAPPublisher):
def soap_quote(self):
return "That beverage, sir, is off the hizzy."
if __name__ == '__main__':
from twisted.internet import reactor
resource
= Quoter()
reactor.listenTCP(
7080, server.Site(resource))
reactor.run()
测试代码

from SOAPpy import *
s
=SOAPProxy('http://127.0.0.1:7080')
print s.quote()
2. XMLRPC

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:
<Fault 123: 'The fault procedure is faulty.'>
>>>
3.JSONRPC,twisted似乎还不支持,但是有很多开源的项目把这个做了。