简单的XMLRPC的Demo

服务器:

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
import time

class Math:
    def pow(self, x, y):
        return x ** y
    
    def hex(self, x):
        return "%x" % x
    
serveraddr = ('', 8888)
print "Server started from ", time.localtime()
srvr = SimpleXMLRPCServer(serveraddr)
srvr.register_instance(Math())
srvr.register_introspection_functions()
srvr.serve_forever()

客户端:

import xmlrpclib, sys

url = "http://localhost:8888"
s = xmlrpclib.ServerProxy(url)

print "Gathering avilable methods..."
methods = s.system.listMethods()

while 1:
    print "\n\nAvailable Methods:"
    for i in range(len(methods)):
        print "%2d: %s" % (i + 1, methods[i])
    selection = raw_input("Select one (q to quit):")
    if selection == 'q':
        break
    item = int(selection) - 1
    print "\n*************"
    print "Detail for %s\n" % methods[item]
    
    for sig in s.system.methodSignature(methods[item]):
        print "Args: %s; Returns: %s" % (", ".join(sig[1:]), sig[0])
    print "Help:", s.system.methodHelp(methods[item]) 

服务器实现了pow和hex两个方法,供调用(真要用,还得加入好些判断,和异常处理)

这个客户端,并不是真正意义上的客户端,只是个万能的XMLRPC的方法列举器。但是至少能说明是连通了

你可能感兴趣的:(简单的XMLRPC的Demo)