XMLRPC服务器端代码,定义了三个方法名,getMonth获取所给月份的日历,getYear获取一年的日历,getDict测试返回字典的效果。
xmlrpc_server.py:
#!/usr/bin/python # coding: utf-8 import calendar, SimpleXMLRPCServer, time, string, os #创建 Server 对象 class Calendar: def getMonth(self, year, month): if(type(year) is type("")): if(year.isdigit()): year=string.atoi(year) else: return "error,the argv must be number" if(type(month) is type("")): if(month.isdigit()): month=string.atoi(month) else: return "error,the argv must be number" return calendar.month(year,month) def getYear(self, year): if(type(year) is type("")): if(year.isdigit()): year=string.atoi(year) else: return "error,the argv must be number" return calendar.calendar(year) def getDict(self): dict={‘1‘:‘1‘,‘2‘:‘2‘,‘3‘:‘3‘} return dict #创建实例 calendar_object = Calendar() server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888)) server.register_instance(calendar_object) #输出信息,等待链接 print "Listening on port 8888" server.serve_forever()
客户端:xmlrpc_client.py:
#!/usr/bin/python # coding: utf-8 import xmlrpclib server = xmlrpclib.ServerProxy("http://localhost:8888") month = server.getMonth (2014,8) print month year = server.getYear(2888) print year dict = server.getDict() print dict for keys in dict.keys(): print "key=%s,value=%s"%(keys,dict[keys])
执行:
chmod u+x xmlrpc_server.py ./xmlrpc_server.py
新的终端:chmod u+x xmlrpc_client.py ./xmlrpc_client.py
效果如图:
Python中的XMLRPC(Calendar)
时间: 2024-10-18 17:27:31