最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

PythonXMLRPC服务器端和客户端实例

来源:动视网 责编:小采 时间:2020-11-27 14:40:26
文档

PythonXMLRPC服务器端和客户端实例

PythonXMLRPC服务器端和客户端实例:一、远程过程调用RPC XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the
推荐度:
导读PythonXMLRPC服务器端和客户端实例:一、远程过程调用RPC XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the
 一、远程过程调用RPC

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. This module supports writing XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.

简单地,client可以调用server上提供的方法,然后得到执行的结果。类似与webservice。

推荐查看xmlprc的源文件:C:\Python31\Lib\xmlrpc

二、实例

1) Server

代码如下:

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

def div(x,y):
return x - y

class Math:
def _listMethods(self):
# this method must be present for system.listMethods
# to work
return ['add', 'pow']
def _methodHelp(self, method):
# this method must be present for system.methodHelp
# to work
if method == 'add':
return "add(2,3) => 5"
elif method == 'pow':
return "pow(x, y[, z]) => number"
else:
# By convention, return empty
# string if no help is available
return ""
def _dispatch(self, method, params):
if method == 'pow':
return pow(*params)
elif method == 'add':
return params[0] + params[1]
else:
raise 'bad method'

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_introspection_functions()
server.register_function(div,"div")
server.register_function(lambda x,y: x*y, 'multiply')
server.register_instance(Math())
server.serve_forever()

2)client

代码如下:

import xmlrpc.client

s = xmlrpc.client.ServerProxy('http://localhost:8000')

print(s.system.listMethods())

print(s.pow(2,3)) # Returns 28
print(s.add(2,3)) # Returns 5
print(s.div(3,2)) # Returns 1
print(s.multiply(4,5)) # Returns 20

3)result

文档

PythonXMLRPC服务器端和客户端实例

PythonXMLRPC服务器端和客户端实例:一、远程过程调用RPC XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the
推荐度:
标签: 客户端 实例 xml
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top