全部博文(343)
分类: Python/Ruby
2011-01-14 16:28:51
def fun(param1, param2):你就可以在你的客户程序中如下简单的调用:
return param1 + param2
import SimpleXMLRPCServer不得不说非常简单了吧!
def fun(param1, param2):
return param1 + param2
srv = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 1234))
srv.register_function(fun)
srv.serve_forever()
import xmlrpc, sys而且客户端程序也有所变化。
PORT = 1234
TEST_NAME = "xing"
TEST_PASS = "123"
TEST_AUTH = False
def doServer():
global exitFlag
exitFlag = 0
s = xmlrpc.server()
if TEST_AUTH:
s.setAuth(authenticate)
s.addMethods({
'echo' : echoMethod,
'exit' : exitMethod,
'hello' : helloMethod,
'getobj': getObjMethod,
'sum' : sumMethod
})
s.bindAndListen(PORT)
while 1:
try:
s.work() # you could set a timeout if desired
except:
e = sys.exc_info()
if e[0] in (KeyboardInterrupt, SystemExit):
raise e[0], e[1], e[2]
traceback.print_exc()
if exitFlag:
break
def authenticate(uri, name, password):
if name == TEST_NAME and password == TEST_PASS:
return (1, 'a domain')
else:
return (0, 'a domain')
# RPC:
def echoMethod(serv, src, uri, method, params):
print 'params are', params
return params
# RPC: Close the server by client side
def exitMethod(serv, src, uri, method, params):
global exitFlag
exitFlag = 1
serv.exit()
return 'okay'
# RPC: Demo function
def helloMethod (serv, src, uri, method, params):
print "Access hello:", serv, ',', src
return "Hello" + params[0] + ", welcome to python world."
#test obj
class Info_Object:
def __init__ (self, name='noname'):
self.name = name
# Globe object for call
info_obj = Info_Object()
def getObjMethod (serv, src, uri, method, params):
params[0].name = info_obj.name
def sumMethod (serv, src, uri, method, params):
sum = 0
for i in range(params[0], params[1]):
sum = sum + i
return sum
if __name__ == '__main__':
print "The XML-RPC server is work on ", PORT, '...'
doServer()