Python 实现 XML-RPC示例

参考1:http://www.ibm.com/developerworks/cn/webservices/ws-pyth/part10/
参考2:http://www.cnblogs.com/coderzh/archive/2008/12/03/1346994.html

服务端:

import SimpleXMLRPCServer
class CRpcServer:
    def __init__(self):
        self.data = "empty"


    def set(self, strData):
        self.data = strData
        print "Setting Data as: [%s]" % strData
        return 0                    #必须要有返回值
        
    def get(self):
        print "Getting Data as: [%s]" % self.data
        return self.data
    
obj = CRpcServer()
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 80))
server.register_instance(obj)


print "Listening on port 80"
server.serve_forever()

客户端

import xmlrpclib
server = xmlrpclib.ServerProxy("http://localhost:80")
server.set("This is setting data")
words = server.get()
print "Getting:" + words

你可能感兴趣的:(Python 实现 XML-RPC示例)